2017-10-19 72 views
-1

我正在寻找一个解决方案来压扁可变多维数组,以将上一个可用数组中的所有值平铺为单个Array[](包含多维数组中的每个最后一个数组的数据集)。可变级别多维数组用PHP值'flattend'数组?

任何帮助表示赞赏!

它不同于其他问题

由于结束结果应该是一个包含所有Arrays的集合:

array(
    'title' => xx, 
    'id' => xx 
) 

在不同多维数组列出。 不同的项目都有固定的keystitleid

示范基地阵列

$data = array(
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     array(
      array(
       'title' => xx, 
       'id' => xx 
      ), 
      array(
       'title' => xx 
       'id' => xx 
      ) 
     ) 
    ), 
    array(
     array(
      array(
       array(
        'title' => xx, 
        'id' => xx 
       ), 
       array(
        'title' => xx, 
        'id' => xx 
       ), 
       array(
        'title' => xx, 
        'id' => xx 
       ) 
      ) 
     ), 
     array(
      'title' => xx, 
      'id' => xx 
     ), 
     array(
      array(
       array(
        array(
         'title' => xx, 
         'id' => xx 
        ) 
       ) 
      ) 
     ) 
    ) 
); 

应flattend到

$data = array(
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ), 
    array(
     'title' => xx, 
     'id' => xx 
    ) 
); 
+0

这不是问题的重复....以展为每个'键的数组'实体...我希望保存'key' /'values'数组... –

+0

有[数百个“flatten array”que stions](https://stackoverflow.com/search?q=%5Bphp%5D+flatten+array)。你有什么具体的尝试,你坚持什么,你的问题与别人有什么不同? – deceze

+0

http://idownvotedbecau.se/noattempt/和http://idownvotedbecau.se/nocode/。帮助你自己,我们会看你的尝试。阅读关于数组的foreach,并且is_array知道该值是数组还是字符串。往下看一个数组,保持键和值,否则。 – Nic3500

回答

1
function flatten_array(&$data) { 

    foreach ($data as $index => &$item) { 

     if(has_array_child($item)) { 
      unset($data[$index]); 
      flatten_array($item); 
      $data = array_merge($data, $item); 
     } 

    } 

} 

function has_array_child($item) { 
    foreach($item as $child) { 
     if(is_array($child)) { 
      return TRUE; 
     } 
    } 

    return FALSE; 
} 

flatten_array($data); 

print_r($data); 
+0

是的,完美!非常感谢!如所须!不需要, –

+0

,$数据通过引用传递。 –

+0

我的不好,忽略了一个!再次感谢 .. –