flatten嵌套数组php?

时间:2014-07-21 18:57:17

标签: php arrays wordpress

好的,请采取以下措施:

    array(80) {
      ["_edit_lock"]=>
      array(1) {
        [0]=>
        string(12) "1405955175:1"
      }
      ["_edit_last"]=>
      array(1) {
        [0]=>
        string(1) "1"
      }
      ["slide_template"]=>
      array(1) {
        [0]=>
        string(7) "default"
      }
      ["pyre_video"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["pyre_full_width"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
   }

我如何将其扁平化为:

array(80) {
  ["_edit_lock"]=>  string(12) "1405955175:1"
  ["_edit_last"]=>  string(1) "1"
  ["slide_template"]=>  string(7) "default"
  ["pyre_video"]=>  string(0) ""
  ["pyre_full_width"]=>   string(2) "no"

}

3 个答案:

答案 0 :(得分:2)

<?php
$res = array();

foreach ($src_array as $key => $value) {
  $res[$key] = $value[0];
}

var_dump($res);
?>

答案 1 :(得分:2)

$flattened = array_map(function($arr){return $arr[0];}, $your_array);

答案 2 :(得分:0)

关于以下几点?

<?php
$myArray = array(
     'first' => array('blabla'),
     'second' => array('blabla'),
);

foreach($myArray as $key => $layer) {
     if(is_array($layer)) {
          $myArray[$key] = $layer[key($myArray)];
     } else { continue; }
}