Pergunta

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.

Foi útil?

Solução

Just switch the column to row,

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

DEMO.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top