连接数组php的递归键

时间:2018-07-02 11:28:52

标签: php arrays recursion key concatenation

我有一个像这样的数组:

array(2) {

  ["xxx"]=>
    array(2) {
      ["yyy"]=>
        array(1) {
          ["text"]=> string(13) "test offline2"
        }
      ["yyy2"]=>
        array(1) {
          ["text"]=> string(12) "test offline"
        }
    }
    ["usual_name"]=> string(12) "test offline"

}

我想要一个像这样的新数组:

array(3) {
  ["xxx.yyy"]=> string(12) "test offline"
  ["xxx.yyy2"]=> string(12) "test offline"
  ["usual_name"]=> string(12) "test offline"
}

如果有人可以帮助我!

1 个答案:

答案 0 :(得分:0)

尝试一下:

$arr1 = array("xxx"=>array(
                        "yyy" => array("text" => "test offline2"),
                        "yyy2" => array("text"=> "test offline")
                    ),
              "usual_name" => "test offline"
        );

$arr2 = array();
foreach ($arr1 as $key1 => $value1) {
    if(is_array($value1)) {
        foreach ($value1 as $key2 => $value2) {
            if(is_array($value2))
            {
                foreach ($value2 as $key3 => $value3) {
                    $arr2[$key1.'.'.$key2]= $value3;
                }
            }
        }
    }
    else {
        $arr2[$key1]= $value1;
    }
}