array_merge_recursive 的使用

作用:递归合并数组

场景:在平时开发中,我们可能遇到获取的数据是多个维度的数据,而我们只需要其中的一个。

代码如下:

$project_id = 'project_id';
$arr1 = [
    'project_id' => ['name' => '名称1', 'address' => '地址1', ],
    'project_id2' => ['name' => '名称2', 'address' => '地址2', ],
];

$arr2 = [
    'project_id' => ['room_num' => '房间数1', 'room_type_num' => '户型数1', ],
    'project_id2' => ['room_num' => '房间数2', 'room_type_num' => '户型数2', ],
];

$arr3 = [
    'project_id' => ['is_new' => '是否为新店1' ],
    'project_id2' => ['is_new' => '是否为新店2'  ],
];

//获取项目id 为 project_id值的数据
$project_data = array_merge_recursive($arr1, $arr2, $arr3)[$project_id];

 

array_merge_recursive($arr1, $arr2, $arr3)的结构是:
Array
(
   [project_id] => Array
       (
           [name] => 名称1
           [address] => 地址1
           [room_num] => 房间数1
           [room_type_num] => 户型数1
           [is_new] => 是否为新店1
       )
   [project_id2] => Array
       (
           [name] => 名称2
           [address] => 地址2
           [room_num] => 房间数2
           [room_type_num] => 户型数2
           [is_new] => 是否为新店2
       )
)

 

 可以看出是以维度 “project_id” 合并的数据,这样我们就可以轻易拿到某一个维度的数据

posted on 2019-12-20 10:47  追风的浪子  阅读(140)  评论(0编辑  收藏  举报

导航