2012-03-12 13 views
1

这个问题是基于我的另一个问题,这里是关于合适的数组处理algorithm将元素的完整位置存储在多维数组中以便重用

在我的情况下,我想展平一个多维数组,但我需要将完整的关键点存储到该元素以供稍后重用。

例如:

array(
    0 => array(
     'label' => 'Item1', 
     'link' => 'http://google.com', 
     'children' => null 
) 

    1 => array(
     'label' => 'Item2', 
     'link' => 'http://google.com', 
     'children' => array(3 => array(
            'label' => 'SubmenuItem1', 
            'link' => 'http://www.yahoo.com', 
            'children' => null 
         ) 
     ) 
) 

    2 => array(
     'label' => 'Item3', 
     'link' => 'http://google.com', 
     'children' => null 
) 
) 

应扁平弄成下表

Key    Link 
=================================== 
[0]    http://google.com 
[1]    http://google.com 
[2]    http://google.com 
[1][3]   http://yahoo.com 

的问题等,是我在我可以很容易地存储在一个多维数组的元素的位置,我发现以后很难找回那个元素。例如,如果我将密钥存储为$key = "[1][3]",则无法使用$myarray[$key]访问它。无论如何要做到这一点?

+0

[Flatten multidimensional array concatenating keys]可能的重复(http://stackoverflow.com/questions/9546181/flatten-multidimensional-array-concatenatingkeys) – Joseph 2012-03-12 08:11:55

+0

由于数组深度不固定,我认为你可以使用(i)'eval'函数 - 可以吸引大量-1s的东西(ii)使用递归 – 2012-03-12 08:25:42

+0

@SalmanA:您可以详细说明如何使用递归吗?投票结束的人:这个问题的主要观点是如何从一个存储的关键字处理多维数组,而不是如何平坦多维数组。 – F21 2012-03-12 09:12:26

回答

1

解决方案使用递归:

//Array parts should be an array containing the keys, for example, to address 
//SubmenuItem1, I had 1.3 when the array was flattened. This was then exploded() to the array [1, 3] 
$this->recurseIntoArray($myArray, $arrayParts); 

private function recurseIntoArray(&$array, $arrayParts){ 
    $current = $arrayParts[0]; 
    $array[$current]['blah'] = 'blah'; //If you want to update everyone in the chain on the way down, do it here 

    array_shift($arrayParts); 

    if (!empty($arrayParts)){ 
     $this->recurseIntoArray($array[$current]['children'], $arrayParts); 
    }else{ 
     //If you want to update only the last one in the chain, do it here. 
    } 
} 
0

假设你有一个数组,其深度不是固定的,你已经想出一个办法来存储在另一个数组的键(使用而不是括号逗号),您可以使用递归遍历数组,如下所示:

# //// SAMPLE INPUT \\\\ 
$array = array(
    0 => array('link' => 'http://google.com'), 
    1 => array('link' => 'http://google.com', 'children' => array(
     3 => array('link' => 'http://www.yahoo.com') 
    )), 
    2 => array('link' => 'http://google.com'), 
    5 => array('link' => 'http://google.com/', 'children' => array(
     6 => array('link' => 'http://google.com/test1/', 'children' => array(
      7 => array('link' => 'http://google.com/test1/test2') 
     )) 
    )) 
); 
$paths = array(
    '0', 
    '1', 
    '1,3', 
    '2', 
    '5', 
    '5,6', 
    '5,6,7' 
); 
# \\\\ SAMPLE INPUT //// 

function getlink($array, $path) { 
    $temp = explode(',', $path, 2); 
    if(count($temp) == 1) { 
     list($this_path) = $temp; 
     return $array[$this_path]['link']; 
    } 
    else { 
     list($this_path, $more_path) = $temp; 
     return getlink($array[$this_path]['children'], $more_path); 
    } 
} 
foreach($paths as $path) { 
    echo getlink($array, $path); 
    echo PHP_EOL; 
} 

递归方法也可用于构建路径数组。