0

I'm working on arrays using php and I am trying to swap items of the array. I have this code:

<html>
<body>
<p>
<?php
    $test2 = array  (   array("!","#","#"),
                        array("@","!","#"),
                        array("@","@","!",)  
                    );

    for($f=0; $f < count($test2); $f++)
    {
        for($g=0; $g < count($test2); $g++)
        {
            if($g >= 2)
            {
            echo "{$test2[$f][$g] } ";          
            }
            else
            {
            echo "{$test2[$f][$g] }- ";         
            }           
        }   
        echo "<br>";
    }

    ...

the code above has an output of:

!- #- #
@- !- #
@- @- ! 

I am trying to swap the index of the arrays so the output will be like this:

!- @- @
#- !- @
#- #- ! 

Thank you guys for the help.

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
user1998735
  • 243
  • 1
  • 3
  • 13
  • So you are trying to reverse the order of both the inner and outer arrays or do you want to leave the arrays intact and simply output them in reverse order? – Mike Brant Feb 12 '14 at 07:20
  • Why you don't decrement in your for loops? http://phpfiddle.org/main/code/kb5-m3x – GuyT Feb 12 '14 at 07:28
  • You could also simplify the `if`. Replace it with: `echo ($g >= 2) ? "{$test2[$f][$g] } " : "{$test2[$f][$g] }- ";` – GuyT Feb 12 '14 at 08:33

1 Answers1

1

Just switch the column to row,

$test2[$f][$g] => $test2[$g][$f]

DEMO.

Rikesh
  • 26,156
  • 14
  • 79
  • 87