Mysql 递归获取多重数组数据

多重数据 $data ,获取*下的所有下级id

$data

array:3 [▼
0 => array:7 [▼
"id" => 1
"created_at" => "2017-08-05 11:30:15"
"updated_at" => "2017-08-05 11:30:15"
"parent_id" => 0
"children" => array:1 [▼
0 => array:7 [▼
"id" => 2
"created_at" => "2017-08-05 11:31:11"
"updated_at" => "2017-08-18 18:57:11"
"parent_id" => 1
"children" => array:1 [▼
0 => array:7 [▼
"id" => 3
"created_at" => "2017-08-05 11:33:34"
"updated_at" => "2017-08-05 11:33:34"
"parent_id" => 2
"children" => []
]
]
]
]
]
1 => array:7 [▼
"id" => 4
"created_at" => "2017-08-10 15:36:03"
"updated_at" => "2017-08-10 15:36:03"
"parent_id" => 0
"children" => array:1 [▼
0 => array:7 [▼
"id" => 5
"created_at" => "2017-08-10 15:36:31"
"updated_at" => "2017-08-10 15:42:08"
"parent_id" => 4
"children" => []
]
]
]
2 => array:7 [▼
"id" => 7
"created_at" => "2017-08-10 15:47:09"
"updated_at" => "2017-08-10 15:50:37"
"parent_id" => 0
"children" => array:1 [▼
0 => array:7 [▼
"id" => 8
"created_at" => "2017-08-10 15:47:20"
"updated_at" => "2017-08-10 15:50:48"
"parent_id" => 7
"children" => array:1 [        0 => array:7 [
            "id" => 9
"created_at" => "2017-08-10 15:49:17"
"updated_at" => "2017-08-10 15:51:08"
"parent_id" => 8
"children" => []
]
]
] ] ] ]

创建一个方法

     public function getCategoryChildrenIds($data, $lev = 0)
{
$arr = [];
foreach ($data as $key => $value) {
if ($lev) {
$arr[] = $value->id;
} if( isset($value['children'])) {
$childLev = $lev + 1;
$arr = array_merge($arr, self::getCategoryChildrenIds($value['children'], $childLev)); } }
return $arr;
}

获取*下所有下级id

array:5 [▼
0 => 2
1 => 3
2 => 5
3 => 8
4 => 9
]
上一篇:CSS详解


下一篇:【Android】安卓四大组件之Activity(二)