【问题标题】:Get all child and grand child of particular parent from given array从给定数组中获取特定父母的所有孩子和孙子女
【发布时间】:2018-08-22 13:31:51
【问题描述】:

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

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

它们都在数组键父级中有关系

任何人都对此有想法! 提前致谢!

【问题讨论】:

标签: php arrays recursion


【解决方案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 )

【讨论】:

  • 谢谢,这会给我带来很好的结果!