【问题标题】:PHP : Dynamically get multidimentional array child element valuePHP:动态获取多维数组子元素值
【发布时间】:2019-09-10 09:58:20
【问题描述】:

我想从数组中自动获取子元素值。它是多维数组格式。您可以看到如下所示的数组格式

注意:子元素会自动生成。所以,不想静态代码逻辑。

我想创建类别树,数组结构如下所示:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_cat_name] => 0
            [cat_name] => Test 1
            [status] => 1
            [position] => 1
            [created_at] => 2019-09-03 09:27:45
            [updated_at] => 2019-09-03 11:00:54
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_cat_name] => 1
                            [cat_name] => Test 2
                            [status] => 1
                            [position] => 2
                            [created_at] => 2019-09-03 09:28:19
                            [updated_at] => 2019-09-03 11:01:00
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [parent_cat_name] => 2
                                            [cat_name] => Test 4
                                            [status] => 1
                                            [position] => 4
                                            [created_at] => 2019-09-03 09:35:20
                                            [updated_at] => 2019-09-03 11:01:03
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 5
                                                            [parent_cat_name] => 4
                                                            [cat_name] => Test 5
                                                            [status] => 1
                                                            [position] => 3
                                                            [created_at] => 2019-09-07 05:55:09
                                                            [updated_at] => 2019-09-07 05:55:09
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 3
            [parent_cat_name] => 0
            [cat_name] => Test 3
            [status] => 1
            [position] => 4
            [created_at] => 2019-09-03 09:35:10
            [updated_at] => 2019-09-03 11:00:58
        )

)

使用的代码:

public function buildTree(array $elements, $parentId = 0) {
        $branch = [];

        foreach ($elements as $element) {
            if ($element['parent_cat_name'] == $parentId) {
                $children = $this->buildTree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }

实际结果:

  • 测试 1
    • 测试 2
      • 测试 4
        • 测试 5
  • 测试 3

【问题讨论】:

  • I want 不是问题。描述您遇到的实际问题。
  • @u_mulder 我无法从上面的数组中生成实际结果。
  • 那么,您使用的代码在哪里?
  • 检查我更新的问题。

标签: php arrays


【解决方案1】:

这里是实现相同的自定义函数,

function custom_function($data)
{
    $result = [];
    // checking if during recursion if data is array or not found
    if (is_array($data) && count($data) > 0) {
        $result[] = '<ul>';
        foreach ($data as $entry) {
            // checking if leaf node visited or not
            if (isset($entry['children'])) {
                $result[] = sprintf('<li>%s %s</li>', $entry['cat_name'], custom_function($entry['children']));
            } else {
                // leaf node visited just add name to li
                $result[] = sprintf('<li>%s</li>', $entry['cat_name']);
            }
        }
        $result[] = '</ul>';
    }
    // implode or ul to form a string
    return implode($result);
}
echo custom_function($arr);

我过去拿了my answer的ref,在某种程度上符合条件。

Demo.

输出:-

<ul>
  <li>Test 1
    <ul>
      <li>Test 2
        <ul>
          <li>Test 4
            <ul>
              <li>Test 5</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Test 3</li>
</ul>

编辑 1

function custom_function($data)
{
    $result = [];
    if (is_array($data) && count($data) > 0) {
        //$result[] = '<ul>';
        foreach ($data as $entry) {
            if (isset($entry['children'])) {
                $result[] = [$entry['cat_name'], custom_function($entry['children'])];
            } else {
                $result[] = $entry['cat_name'];
            }
        }
        //$result[] = '</ul>';
    }
    return ($result);
}
$data= custom_function($arr);
print_r($data);

输出

Array
(
    [0] => Array
        (
            [0] => Test 1
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Test 2
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Test 4
                                            [1] => Array
                                                (
                                                    [0] => Test 5
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Test 3
)

【讨论】:

  • 返回:未定义索引:children
  • 我进行了更改,现在刷新页面并检查我的答案的编辑版本。
  • 看起来很完美。您能否以数组而不是 li 标签的形式返回。
  • 您的输出与我的匹配。这就是你想要的对吗?如果您愿意,请删除最后一个内爆条件并根据您的要求使用该数组
  • 你太棒了!!谢谢你帮助我。接受!!
【解决方案2】:
<?php

        subarray($a);
        function subarray($aa){
            for($i=0;$i<count($aa);$i++){
                echo $aa[$i]['cat_name']."<BR>";
                if(array_key_exists("children",$aa[$i])){
                    $charr=$aa[$i]['children'];
                    subarray($charr);
                }               
            }
        }


?>

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2020-03-29
    • 2017-08-05
    相关资源
    最近更新 更多