Php 从给定数组中获取特定父级的所有子级和孙子级
我有一个所有集合的数组,我需要得到给定父集合的子集合和孙子集合作为数组Php 从给定数组中获取特定父级的所有子级和孙子级,php,arrays,recursion,Php,Arrays,Recursion,我有一个所有集合的数组,我需要得到给定父集合的子集合和孙子集合作为数组 [0] => Array ( [id] => 61 [name] => Fashion [slug] => fashion [desc] => [access] => 1 [deleted_at] => [created_at] => 2018-08-22
[0] => Array
(
[id] => 61
[name] => Fashion
[slug] => fashion
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:38:50
[updated_at] => 2018-08-22 18:38:50
[parent] => 0
)
[1] => Array
(
[id] => 62
[name] => Mens
[slug] => mens
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:39:01
[updated_at] => 2018-08-22 18:39:01
[parent] => fashion
)
[2] => Array
(
[id] => 63
[name] => Watch
[slug] => watch
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:39:10
[updated_at] => 2018-08-22 18:39:10
[parent] => mens
)
[3] => Array
(
[id] => 64
[name] => Shoe
[slug] => shoe
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:39:30
[updated_at] => 2018-08-22 18:39:30
[parent] => mens
)
[4] => Array
(
[id] => 65
[name] => Formal Shoe
[slug] => formal-shoe
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:39:42
[updated_at] => 2018-08-22 18:39:42
[parent] => shoe
)
[5] => Array
(
[id] => 66
[name] => Casual Shoe
[slug] => casual-shoe
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:39:54
[updated_at] => 2018-08-22 18:39:54
[parent] => shoe
)
[6] => Array
(
[id] => 67
[name] => Womens
[slug] => womens
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:40:04
[updated_at] => 2018-08-22 18:40:04
[parent] => fashion
)
[7] => Array
(
[id] => 68
[name] => Dress
[slug] => dress
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:40:15
[updated_at] => 2018-08-22 18:40:15
[parent] => womens
)
[8] => Array
(
[id] => 69
[name] => Electrical and Electronics
[slug] => electrical-and-electronics
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:40:34
[updated_at] => 2018-08-22 18:40:34
[parent] => 0
)
[9] => Array
(
[id] => 70
[name] => Television
[slug] => television
[desc] =>
[access] => 1
[deleted_at] =>
[created_at] => 2018-08-22 18:40:46
[updated_at] => 2018-08-22 18:40:46
[parent] => electrical-and-electronics
)
我需要给定数组的以下结果
**输入父项:**fashion
**预期结果:**
array('mens','womens','dress','shoe','casual-shoe','formal-shoe','watch')
array('dress')
array('shoe','casual-shoe','formal-shoe','watch')
**输入家长:**女性
**预期结果:**
array('mens','womens','dress','shoe','casual-shoe','formal-shoe','watch')
array('dress')
array('shoe','casual-shoe','formal-shoe','watch')
**输入父项:*男项
**预期结果:**
array('mens','womens','dress','shoe','casual-shoe','formal-shoe','watch')
array('dress')
array('shoe','casual-shoe','formal-shoe','watch')
它们在数组键父级中都有关系
任何人对此都有想法!
提前谢谢 我的解决方案,不确定是否是最好的,是使用补偿函数来查找父母和父母的父母
function findParent($array,$parentName,$result=[],$startingDepth=0,$maxDepth=2){
foreach($array as $arr){
if($arr["parent"] == $parentName && $startingDepth < $maxDepth){
$result[] = $arr["slug"];
$parentOfParent = findParent($array, $arr["slug"],$result,$startingDepth,$startingDepth+1);
$res = array_merge($result,$parentOfParent);
$result = array_unique($res);
}
}
return $result;
}
我强烈建议您使用PHP对象,而不是现有的数组系统。这会让你的生活更轻松。到目前为止你都试了些什么?谢谢,这会给我很大的效果!