多重数据 $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" => []
]
]
] ] ] ]
创建一个方法
1 public function getCategoryChildrenIds($data, $lev = 0)
2 {
3 $arr = [];
4 foreach ($data as $key => $value) {
5 if ($lev) {
6 $arr[] = $value->id;
7 }
8
9 if( isset($value['children'])) {
10 $childLev = $lev + 1;
11 $arr = array_merge($arr, self::getCategoryChildrenIds($value['children'], $childLev));
12
13 }
14
15 }
16 return $arr;
17 }
获取*下所有下级id
array:5 [▼
0 => 2
1 => 3
2 => 5
3 => 8
4 => 9
]