user1998735
user1998735

Reputation: 253

how to swap elements in a multidimensional array in php

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.

Upvotes: 0

Views: 1290

Answers (1)

Rikesh
Rikesh

Reputation: 26441

Just switch the column to row,

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

DEMO.

Upvotes: 1

Related Questions