多维数组问题

时间:2011-02-11 09:52:53

标签: php

我有像这样的数组输入

Array
(
    [col1] => Array
        (
            [208] => Array
                (
                    [0] => 264
                )

        )

    [col2] => Array
        (
            [208] => Array
                (
                    [0] => 251
                    [1] => 252
                    [2] => 255
                )

        )

    [col4] => Array
        (
            [208] => Array
                (
                    [0] => 497
                )

        )

    [col6] => Array
        (
            [208] => Array
                (
                    [0] => 505
                )

        )

)

我想创建一个函数可以是进程数组输入然后显示输出数组,如

Array
(
    [0] => Array
        (
            [0] => 208
        )
    [208] => Array
        (            
            [0] => 264           
        )
    [264] => Array
        (            
            [0] => 251
            [1] => 252
            [2] => 255            
        )
    [251] => Array
        (            
            [0] => 497            
        )
    [252] => Array
        (            
            [0] => 497            
        )
    [255] => Array
        (            
            [0] => 497            
        )
    [497] => Array
        (            
            [0] => 505            
        ) 

)

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

$input = array('col1' => array(208 => array(264)),
                'col2' =>array(208 => array(251,252,255)),
                'col4' =>array(208 => array(497)),
                'col6' =>array(208 => array(505)));

$temp = array();
$output = array();
foreach($input as $worker) {
    foreach($worker as $key => $worker2) {
        if (count($temp) == 0) {
            $temp[][] = $key;
            $lastInserted = $key;
        }
        foreach($worker2 as $worker3) {
            $temp[$lastInserted][] = $worker3;
            $tempLast =$worker3;
        }
        $lastInserted = $tempLast;
    }
}
echo "input:\n";
print_r($input);
echo "output:\n";
print_r($temp);

产生完全相同的结果。

答案 1 :(得分:0)

感谢你的功能gnur,我编辑你的功能以获得正确的输出,然后完成这个问题。

 array(208 => array(264)),
                    'col2' =>array(208 => array(251,252,255)),
                    'col4' =>array(208 => array(497)),
                    'col6' =>array(208 => array(505)));

$temp = array();
$output = array();
foreach($input as $k => $worker) {
    foreach($worker as $key => $worker2) {
        if (count($temp) == 0) {
            $temp[][] = $key;
            $lastInserted[] = $key;                
        }
        $tempLast = array();
        foreach($worker2 as $worker3) {
            foreach($lastInserted as $worker4) {
                if(!in_array($worker3, $temp[$worker4])){
                    $temp[$worker4][] = $worker3;
                    $tempLast[] = $worker3;        
                }                    
            }                                                
        }
        $lastInserted = $tempLast;            
    }
}
echo "<pre>";
echo "input:\n";
print_r($input);
echo "output:\n";
print_r($temp);
?>