多维数组通过索引形成回声

时间:2011-07-01 13:18:38

标签: php

Array
(
    [1] => Array
        (
            [0] => 1
            [1] => none
            [2] => none
        )

    [2] => Array
        (
            [0] => none
            [1] => 2
            [2] => none
        )

    [3] => Array
        (
            [0] => none
            [1] => 2
            [2] => 3
        )

)

你好专家我从复选框中得到这个数组,我希望通过索引值来回显它,即按键的顺序,例如一个口袋中的所有0个索引值和一个口袋中的1个索引值,例如[[0, 0, 0], [1, 1, 1], [2, 2, 2]] with its values like [[1, none, none], [none, 2, 2], [none, none, 3]] 请有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

$myArray = array(   1 => array( 0 => 1, 1 => "none", 2 => "none"),                   
                    2 => array( 0 => "none", 1 => 2, 2 => "none"),                   
                    5 => array( 0 => "none", 1 => 2, 2 => 3) );  

$newArray = array();
foreach( $myArray AS $masterIndex => $childArray )
{
    foreach( $childArray AS $index => $value )     
    {
        if( !isset($newArray[$index]) )
        {
            $newArray[$index] = array();
        }
        $newArray[$index][] = $value;
    }
}

$outputValues = "["; 

foreach( $newArray AS $childArray ) 
{     
    $outputValues .= "[" . implode(",", $childArray) . "],";
} 
$outputValues = substr($outputValues, 0, strlen($outputValues) -1) . "]";
echo $outputValues;

这是稍微改进的版本,带有'implode'而不是另一个循环......