从给定数组中获取特定父代的所有子代和孙子

时间:2018-08-22 13:31:52

标签: php arrays recursion

我有一个所有集合的数组,我需要将给定父级的子代和孙子作为数组。

[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
    )

我需要给定数组的以下结果。

  

**输入父母:**时尚

     

**预期结果:**

array('mens','womens','dress','shoe','casual-shoe','formal-shoe','watch')
     

**输入父母:**女性

     

**预期结果:**

array('dress')
     

**输入父母:**男士

     

**预期结果:**

array('shoe','casual-shoe','formal-shoe','watch')

所有这些在数组键父级中都有关系

任何人对此都有一个想法! 预先感谢!

1 个答案:

答案 0 :(得分:1)

我的解决方案(不确定最好的解决方案)是使用游览功能来查找父母和父母的父母。

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;
}

测试

echo "fashion:<br/>";
print_r(findParent($data,"fashion"));
echo "<br/>womens:<br/>";
print_r(findParent($data,"womens"));
echo "<br/>mens:<br/>";
print_r(findParent($data,"mens"));

输出(不按找到的顺序):

  fashion:
    Array ( [0] => mens [1] => watch [2] => shoe [3] => formal-shoe [4] => casual-shoe [5] => womens [12] => dress ) 
    womens:
    Array ( [0] => dress ) 
    mens:
    Array ( [0] => watch [1] => shoe [4] => formal-shoe [5] => casual-shoe )