我有一个由父母和孩子组成的类别数组。有人可以帮助我使用parent_id和category_id与foreach循环在层次结构中排列数组。最顶层的Parent的parent_id为“0”。请忽略除category_id和parent_id之外的其他字段。这是我的阵列。
Array
(
[categories] => Array
(
[0] => Array
(
[workspace_id] => 1
[category_id] => 1527
[category_name] => CABIN
[category_description] => a seperate cabin
[capacity] => 100
[category_price] => 50000
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 0
[is_parent] => 0
)
[1] => Array
(
[workspace_id] => 1
[category_id] => 1528
[category_name] => C3
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1527
[is_parent] => 0
)
[2] => Array
(
[workspace_id] => 1
[category_id] => 1529
[category_name] => FLOOR3
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1528
[is_parent] => 0
)
[3] => Array
(
[workspace_id] => 1
[category_id] => 1530
[category_name] => CABIN
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 0
[is_parent] => 0
)
[4] => Array
(
[workspace_id] => 1
[category_id] => 1531
[category_name] => C3
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1530
[is_parent] => 0
)
[5] => Array
(
[workspace_id] => 1
[category_id] => 1532
[category_name] => FLOOR1
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1531
[is_parent] => 0
)
[6] => Array
(
[workspace_id] => 1
[category_id] => 1533
[category_name] => CABIN
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 0
[is_parent] => 0
)
[7] => Array
(
[workspace_id] => 1
[category_id] => 1534
[category_name] => C5
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1533
[is_parent] => 0
)
[8] => Array
(
[workspace_id] => 1
[category_id] => 1535
[category_name] => FLOOR1
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1534
[is_parent] => 0
)
[9] => Array
(
[workspace_id] => 1
[category_id] => 1536
[category_name] => CABIN
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 0
[is_parent] => 0
)
[10] => Array
(
[workspace_id] => 1
[category_id] => 1537
[category_name] => C3
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1536
[is_parent] => 0
)
[11] => Array
(
[workspace_id] => 1
[category_id] => 1538
[category_name] => FLOOR2
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1537
[is_parent] => 0
)
[12] => Array
(
[workspace_id] => 1
[category_id] => 1539
[category_name] => CABIN
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 0
[is_parent] => 0
)
[13] => Array
(
[workspace_id] => 1
[category_id] => 1540
[category_name] => ABOVE SBI
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1539
[is_parent] => 0
)
[14] => Array
(
[workspace_id] => 1
[category_id] => 1541
[category_name] => FLOOR2
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1540
[is_parent] => 0
)
[15] => Array
(
[workspace_id] => 1
[category_id] => 1542
[category_name] => Launch Pad
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 0
[is_parent] => 0
)
[16] => Array
(
[workspace_id] => 1
[category_id] => 1543
[category_name] => C5
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1542
[is_parent] => 0
)
[17] => Array
(
[workspace_id] => 1
[category_id] => 1544
[category_name] => FLOOR3
[category_description] =>
[capacity] =>
[category_price] =>
[status] => 1
[created_by] => 1
[created_time] => 2017-07-14 07:56:54
[updated_by] => 1
[updated_time] => 2017-07-14 07:56:54
[label_id] => 1
[parent_id] => 1543
[is_parent] => 0
)
)
)
答案 0 :(得分:0)
考虑功能
function get_childs($arr, $parent_id = 0) {
$res = array();
foreach ($arr as $row) {
if ($parent_id == $row['parent_id']) {
$childs = get_childs($arr, $row['category_id']);
$row['childs'] = $childs;
$res[] = $row;
}
}
return $res;
}
然后使用此功能获取父母及其孩子
$res = get_childs($arr);