0

i have one php array
and i want to draw output of this array in something like deciding type.
Here is my php code

<?php
$data = array('A','B','C','D','E','F');
$count = count($data);
for($k = 0;$k<$count;$k++){
    foreach($data as $key => $value){
        if($key == $k){
            $datanew = $count - $k;
            for($i=0 ; $i<$datanew ; $i++){
                echo "X";
            }
        }else{
            echo "V";
        }
    }
    echo "</br>";
}
?>

current output

XXXXXXVVVVV
VXXXXXVVVV
VVXXXXVVV
VVVXXXVV
VVVVXXV
VVVVVX

excepted output

XXXXXX
VXXXXX
VVXXXX
VVVXXX
VVVVXX
VVVVVX

insort after X no V

what logic i want to implicit to get perfect output.
thanks

2 Answers 2

2

I hope you're happy with the following solution:

<?php

$data = array('A', 'B', 'C', 'D', 'E', 'F');
$count = count($data);
for ($k = 0; $k < $count; $k++) {
    echo str_repeat("V", $k);
    echo str_repeat("X", $count-$k);
    echo "<br />";
}
?>

I used str_repeat to repeat the chars X and V. So you just need only one for-loop.

Output:

XXXXXX
VXXXXX
VVXXXX
VVVXXX
VVVVXX
VVVVVX

Example on Ideone.com

Sign up to request clarification or add additional context in comments.

Comments

0

Simple just add break in your code as below:

    <?php
     $data = array('A','B','C','D','E','F');
     $count = count($data);
     for($k = 0;$k<$count;$k++){
       foreach($data as $key => $value){
          if($key == $k){
            $datanew = $count - $k;
               for($i=0 ; $i<$datanew ; $i++){
                  echo "X";
                }
               break;
             }
          else{
            echo "V";
           }
        }
       echo "</br>";
     }
   ?>

And output as you wanted

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.