【问题标题】:Recursion array of categories an subcategories PHP类别的递归数组和子类别 PHP
【发布时间】:2021-11-24 06:37:00
【问题描述】:

我有一系列类别,其中包含许多子类别,每个子类别都有其子类别,依此类推...

array(1) {
["categories"]=>array(11) {
[0]=>array(4) {
  ["id"]=>int(368)
  ["name"]=>string(8) "Aksesuar"
  ["parentId"]=>NULL
  ["subCategories"]=>array(15) {
    [0]=>
    array(4) {
      ["id"]=>
      int(387)
      ["name"]=>
      string(4) "Saat"
      ["parentId"]=>
      int(368)
      ["subCategories"]=>
      array(0) {
      }
    }
    [1]=>
    array(4) {
      ["id"]=>
      int(394)
      ["name"]=>
      string(6) "Şapka"
      ["parentId"]=>
      int(368)
      ["subCategories"]=>
      array(0) {
      }
    }
    [2]=>
    array(4) {
      ["id"]=>
      int(396)
      ["name"]=>
      string(17) "Takı & Mücevher"
      ["parentId"]=>
      int(368)
      ["subCategories"]=>
      array(17) {
        [0]=>
        array(4) {
          ["id"]=>
          int(397)
          ["name"]=>
          string(8) "Bileklik"
          ["parentId"]=>
          int(396)
          ["subCategories"]=>
          array(7) {
            [0]=>
            array(4) {
              ["id"]=>
              int(1238)
              ["name"]=>
              string(15) "Altın Bileklik"
              ["parentId"]=>
              int(397)
              ["subCategories"]=>
              array(0) {
              }
            }

每个子类别都有 parentId 表示父类别,大多数顶级父类别的父 id 为 null。我需要一个递归函数,我会将参数作为子类别的 id 和类别数组提供,它会返回父类别数组及其子类别。

function recursive($needle, $array, $id, $holder = [])
{
$holder = [];
foreach ($array as $key => $value) {
    if ($key == $needle && $value == $id) {
        $holder = array_merge($holder, $array);
    }
    if (is_array($value)) {
        $holder = array_merge($holder, recursive($needle, $value, $id, $holder));
    }
}

return $holder;
}
$res = recursive('id', $categories, 5208);

上面的函数只返回父类别及其子类别的数组。

【问题讨论】:

  • 您能否进一步说明您需要的结果。让我们获取您提供的数据。如果你调用'recursive('id', $categories, 397)',你是否期望从根到找到的元素的整个树(所以368-> 396-> 397)?找到的类别(1238)的子类别是否也需要返回?
  • @vixducis 当您通过 397 时,您将仅获得此类别及其子类别,我需要的是获得包括 397 个父类别的整个树。例如,当我通过最低子类别时,我需要获取其所有父类别,如果上述数组中最低的是 1238,它应该返回我 368->396->397->1238
  • 好的,明白了,但是找到的类别的子类别也需要返回吗?
  • @vixducis 不,只有子类别的父类别

标签: php arrays recursion


【解决方案1】:

我不完全确定 'holder' 函数变量的用途是什么,因为您在每次调用时将其重置为空数组,所以我将其完全删除。

function recursive(string $needle, array $categories, $id): ?array
{
    foreach ($categories as $cat) {
        if (array_key_exists($needle, $cat) && $cat[$needle] === $id) {
            // make sure that if correct category is found,
            // no subcategories will be included
            unset($cat['subCategories']);
            return $cat;
        } else {
            $subCat = recursive($needle, $cat['subCategories'], $id);
            // if the $subCat is not null, it indicates the category 
            // with the correct id, or one of it's parent has been found
            if ($subCat !== null) {
                $cat['subCategories'] = $subCat;
                return $cat;
            }
        }
    }
    return null;
}

实际上并没有那么复杂,如果在其中找到 ID,则该函数返回一个数组,如果是子 ID,则返回一个数组,如果没有找到,则返回 NULL。

该函数循环遍历提供的关卡的类别,并检查当前关卡是否包含提供的 ID。如果没有,它将搜索所有子类别。如果在子类别中找到某些内容,它将用返回的子类别替换当前的子类别,并返回类别本身。

如果您将一个类包装在一个类别周围,这可能会更简洁,但这应该可以完成工作。如果您还有其他问题,请告诉我。

查看this 3v4l 以获得完整的复制案例。

【讨论】:

  • 这很好用,谢谢
猜你喜欢
  • 1970-01-01
  • 2021-02-19
  • 2016-07-18
  • 1970-01-01
  • 2017-09-19
  • 2012-03-18
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多