PHP Array GET父级和子级

时间:2018-07-21 15:07:59

标签: php

你好,我从数据集中得到了这个数组:

示例:

array (
  1 => 
  array (
    'id' => '1',
    'name' => ' Category',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  2 => 
  array (
    'id' => '2',
    'name' => ' Slippers',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  3 => 
  array (
    'id' => '3',
    'name' => ' TShirts',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  4 => 
  array (
    'id' => '4',
    'name' => ' BlousesSweatshirt',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  5 => 
  array (
    'id' => '5',
    'name' => ' Cap',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  6 => 
  array (
    'id' => '6',
    'name' => ' Stickers',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  7 => 
  array (
    'id' => '7',
    'name' => ' ScreensandFrames',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  8 => 
  array (
    'id' => '8',
    'name' => ' Models',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  9 => 
  array (
    'id' => '9',
    'name' => ' Notebook',
    'id_parent' => '6',
    'ativo' => '1',
  ),
  10 => 
  array (
    'id' => '10',
    'name' => ' Door',
    'id_parent' => '0',
    'ativo' => '1',
  ),
  11 => 
  array (
    'id' => '11',
    'name' => ' Door',
    'id_parent' => '6',
    'ativo' => '1',
  ),
  12 => 
  array (
    'id' => '12',
    'name' => ' Kangaroo',
    'id_parent' => '4',
    'ativo' => '1',
  ),
  13 => 
  array (
    'id' => '13',
    'name' => ' KangarooRaglan',
    'id_parent' => '4',
    'ativo' => '1',
  ),
  14 => 
  array (
    'id' => '14',
    'name' => ' RoundCollar',
    'id_parent' => '4',
    'ativo' => '1',
  ),
  15 => 
  array (
    'id' => '15',
    'name' => ' Trucker',
    'id_parent' => '5',
    'ativo' => '1',
  ),
  16 => 
  array (
    'id' => '16',
    'name' => ' Basic',
    'id_parent' => '3',
    'ativo' => '1',
  ),
  17 => 
  array (
    'id' => '17',
    'name' => ' Longline',
    'id_parent' => '3',
    'ativo' => '1',
  ),
  18 => 
  array (
    'id' => '18',
    'name' => ' Raglan',
    'id_parent' => '3',
    'ativo' => '1',
  ),
  19 => 
  array (
    'id' => '19',
    'name' => ' Raglan3/4',
    'id_parent' => '3',
    'ativo' => '1',
  ),
  20 => 
  array (
    'id' => '20',
    'name' => ' Regatta',
    'id_parent' => '3',
    'ativo' => '1',
  ),
  21 => 
  array (
    'id' => '21',
    'name' => ' Slide',
    'id_parent' => '2',
    'ativo' => '1',
  ),
  22 => 
  array (
    'id' => '22',
    'name' => ' Stickers',
    'id_parent' => '8',
    'ativo' => '1',
  ),
  23 => 
  array (
    'id' => '23',
    'name' => ' Notebook',
    'id_parent' => '22',
    'ativo' => '1',
  ),
  24 => 
  array (
    'id' => '24',
    'name' => ' T-shirt',
    'id_parent' => '8',
    'ativo' => '1',
  ),
  25 => 
  array (
    'id' => '25',
    'name' => ' Basic',
    'id_parent' => '24',
    'ativo' => '1',
  ),
  26 => 
  array (
    'id' => '26',
    'name' => ' Slippers',
    'id_parent' => '8',
    'ativo' => '1',
  ),
  27 => 
  array (
    'id' => '27',
    'name' => ' Slide',
    'id_parent' => '26',
    'ativo' => '1',
  ),
  28 => 
  array (
    'id' => '28',
    'name' => ' 1Screen',
    'id_parent' => '7',
    'ativo' => '1',
  ),
  29 => 
  array (
    'id' => '29',
    'name' => ' Set3Screens',
    'id_parent' => '7',
    'ativo' => '1',
  ),
  30 => 
  array (
    'id' => '30',
    'name' => ' Set5Screens',
    'id_parent' => '7',
    'ativo' => '1',
  ),
  31 => 
  array (
    'id' => '31',
    'name' => ' BlousesSweatshirt',
    'id_parent' => '8',
    'ativo' => '1',
  ),
  32 => 
  array (
    'id' => '32',
    'name' => ' Cap',
    'id_parent' => '8',
    'ativo' => '1',
  ),
  33 => 
  array (
    'id' => '33',
    'name' => ' ScreensandFrames',
    'id_parent' => '8',
    'ativo' => '1',
  ),
)

这个数组有父子,

示例:

-EX:[1]模特的父亲:贴纸,T恤,拖鞋

-EX:[2]贴纸的父亲是:笔记本

我需要的是递归获取Model和所有Childrens的函数:

例如,我已经完成了从笔记本获取递归父亲的功能:

此处:

function returnParent($id, $haystack, $arr = null){

    $needle = $haystack[$id];

    $arr[] = $needle;

        if($needle['id_parent']){
            return returnParent($needle['id_parent'], $haystack, $arr);
        }else{
            return $arr;
        }
}

其中$id = 23$haystack是完整的数据集。

结果:

Array
(
    [0] => Array
        (
            [id] => 23
            [name] => Notebook
            [id_parent] => 22
        )

    [1] => Array
        (
            [id] => 22
            [name] => Stickers
            [id_parent] => 8
        )

    [2] => Array
        (
            [id] => 8
            [name] => models
            [id_parent] => 0
        )

)

返回孩子的函数的结果必须与returnParent相同,查找的值必须在数组中作为键0;

完整的数据集在这里: https://pastebin.com/jgTM7aLA

建议使我的功能更好:)


编辑

预期输出:

  0 => 
  array (
    'id' => '8',
    'name' => ' Models',
    'id_parent' => '0',
    'ativo' => '1',
  ),

  1 => 
  array (
    'id' => '6',
    'name' => ' Stickers',
    'id_parent' => '0',
    'ativo' => '1',
  ),

  2 => 
  array (
    'id' => '9',
    'name' => ' Notebook',
    'id_parent' => '6',
    'ativo' => '1',
  ),

  3 => 
  array (
    'id' => '24',
    'name' => ' T-shirt',
    'id_parent' => '8',
    'ativo' => '1',
  ),

  4 => 
  array (
    'id' => '25',
    'name' => ' Basic',
    'id_parent' => '24',
    'ativo' => '1',
  )... and ..etc

类似的事情,键0必须是搜索到的值,而其他值则不必按树顺序排列,任何顺序都可以,只要获取所有值即可。

换句话说:

获取一个数组节点,向他和所有子孙展示。

2 个答案:

答案 0 :(得分:2)

function returnChild($id, $haystack, $arr = []){

$needle = $haystack[$id];
array_push($arr, $needle);
foreach($haystack as $key){
    if($key['id_parent'] == $needle['id']){
        array_push($arr,returnChild($key['id'], $haystack));
    }
}
return $arr;
}

我希望这是解决您问题的方法!

答案 1 :(得分:1)

我相信这就是您想要的。
我循环唯一的父母,并找到哪些项目具有匹配的父母。
我必须为父母和ID添加一个“ NULL”值,因为您的数组的索引不为零。
我花了很长时间才弄清楚...

无论如何,返回的是一个“父”数组和下面的子级。

$temp = array_column($arr, "id");
$ids[0] = "NULL";
$ids = array_merge($ids, $temp);

$temp = array_column($arr, "id_parent");
$parents[0] = "NULL";
$parents = array_merge($parents, $temp);


Foreach(array_unique($parents) as $parent){
    if($parent ==0) continue; // if parent is 0 we don't need to search for it (I think)
    $new[$parent]['parent'] = $arr[$parent];
    $new[$parent]['children'] = array_intersect_key($arr, array_intersect($parents, [$parent]));
}
Var_dump($new);

https://3v4l.org/Fbet4