PHP数组获取父级和子级

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (255)

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

例:

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]贴纸是笔记本的父亲

我需要的是一个函数来递归模型和所有Childrens(直到结束):

我已经完成了从Notebook获取Recursive Fathers的函数,例如:

这里:

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

where $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必须是搜索值,其他值,不需要按树顺序,任何顺序都可以,只要获取所有值。

换一种说法:

得到一个阵列节点,展示他和所有的孩子和孙子。

提问于
用户回答回答于
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;
}

我希望这是你问题的解决方案!

用户回答回答于

我相信这就是你要找的东西。 我循环独特的父母并找到哪些项目具有匹配的父项。 我必须向父节点和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

扫码关注云+社区

领取腾讯云代金券