简体   繁体   中英

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.

Just switch the column to row,

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

DEMO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM