如何将多维数组转换为更简单的结构?

时间:2014-07-31 14:35:34

标签: php arrays multidimensional-array

我有一个嵌套的多维数组,如下所示:

   Array (
        [1] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [catName] => Villes
                                [catUrl] => villes
                                [parent] => 151
                            )

                        [1] => Array
                            (
                                [0] => Array
                                    (
                                        [catName] => Administratif
                                        [catUrl] => territoire
                                        [parent] => 37
                                    )

                                [1] => Array
                                    (
                                        [0] => Array
                                            (
                                                [catName] => Gegraphie
                                                [catUrl] => geographie
                                                [parent] => 0
                                            )

                                    )

                            )

                    )
    )

我想将它重新组合成更简单的级别,如下所示:

    Array (
        [1] => Array
            (
                     [0] => Array
                            (
                                [catName] => Villes
                                [catUrl] => villes
                                [parent] => 151
                            )

                     [1] => Array
                             (
                                [catName] => Administratif
                                [catUrl] => territoire
                                [parent] => 37
                             )

                     [2] => Array
                            (
                               [catName] => Gegraphie
                               [catUrl] => geographie
                               [parent] => 0
                            )

    )
)

我想它可以使用一些递归函数,但我的技能并不是我最好的。我怎么能做到这一点?

4 个答案:

答案 0 :(得分:1)

这是一种方法。此功能将折叠每个级别:

function collapse($array) {
    // End of recursion is indicated when the first element is not an array.
    if (!is_array(reset($array))) {
        return array($array);
    }
    // Otherwise, collapse it.
    return array_reduce($array, function($carry, $item) {
        // Recursively collapse each item and merge them together.
        return array_merge($carry, collapse($item));
    }, array());
}

它可以像这样应用于你的数组:

$collapsed = array_map("collapse", $array);

答案 1 :(得分:0)

它不漂亮,但它有效:

$deeparray = array();  // the really nested array goes here
$flattened = array();

function flatten($item,$key)
{
  global $flattened;
  if ( $key == 'catName' || $key == 'catUrl' || $key == 'parent' )
  {
    if ( sizeof( $flattened) == 0 )
    {  $flattened[] = array( $key=>$item );  }
    else
    {
      $last = array_pop($flattened);
      if ( array_key_exists($key,$last) )
      {
        $flattened[] = $last;
        $flattened[] = array( $key=>$item );
      }
      else
      {
        $last[ $key ] = $item;
        $flattened[] = $last;
      }
    }
  }
}



array_walk_recursive($deeparray,'flatten',$flattened);

$flattened = array($flattened);

答案 2 :(得分:-1)

你可以尝试

foreach($toplevel as $a){
    $finalarray = $a[0];
}

答案 3 :(得分:-1)

如果结构总是与您所显示的相同,那么我认为您可以这样做:

$newarray[1][0] = $oldarray[1][0][0];  
$newarray[1][1] = $oldarray[1][0][1][0];  
$newarray[1][2] = $oldarray[1][0][1][1][0];