简体   繁体   English

如何在php中交换多维数组中的元素

[英]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. 我正在使用php处理数组,并且试图交换数组中的项。 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 . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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