在php中将多维数组转换为单个数组

时间:2014-06-11 07:22:17

标签: php multidimensional-array

我有

Array(
      [0] => Array(
        [0] => My Property
        [1] => Array(
            [0] => Tiles Market
            [1] => My Floor
            )
        )
)

我想要

array([0]=>My Property [1] => Tiles Market [2] => My Floor)

3 个答案:

答案 0 :(得分:0)

$arr = array(); // Your array content

$merged_array = array();
$ri = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($ri as $r) {
   array_push($merged_array,$r);
}

echo "<pre>"; print_r($merged_array);

答案 1 :(得分:0)

试试这个

$new_array = call_user_func_array('array_merge', $old_array);

答案 2 :(得分:0)

<强> Demo

您可以使用下面的RecursiveArrayIterator执行此操作;

<?php
function MultiToOne($from, &$to) {
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($from));
    foreach($it as $k => $v) {
      $to[] = $v;
    }   
}


$arr = array(
      "0" => array(
        "0" => "My Property",
        "1" => array(
            "0" => "Tiles Market",
            "1" => "My Floor"
            )
        )
);

$myFinalArr = array();
MultiToOne($arr, $myFinalArr);

var_dump($myFinalArr);

?>