【问题标题】:Manipulating a multidimensional array操作多维数组
【发布时间】:2018-02-27 20:10:58
【问题描述】:

我正在尝试操纵多级类别树的结果,该树动态地为我提供如下所示的对象数组:

Array(
[0] => stdClass Object
    (
        [id] => 1
        [children] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 2
                    )

                [1] => stdClass Object
                    (
                        [id] => 3
                    )

            )

    )

[1] => stdClass Object
    (
        [id] => 9
    )

[2] => stdClass Object
    (
        [id] => 4
        [children] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 8
                    )

                [1] => stdClass Object
                    (
                        [id] => 5
                        [children] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 6
                                    )

                                [1] => stdClass Object
                                    (
                                        [id] => 7
                                    )

                            )

                    )

            )

    ))

它基本上告诉我们对象的 id 以及它是否有任何子对象。很像 Wordpress 中的菜单生成器。

我正在尝试将这个数组转换为一个新的 2 级数组,如下所示:

Array(
[0] => Array
    (
        [0] => 1
        [1] => 9
        [2] => 4
    )

[1] => Array
    (
        [0] => 2
        [1] => 3
    )

[4] => Array
    (
        [0] => 8
        [1] => 5
    )

[5] => Array
    (
        [0] => 6
        [1] => 7
    ))

第一级的第一个键 ([0]) 将是没有任何父母的 id。所以 ids 1、9 和 4 是主要类别。而子序列的将是那些是父母的。所以,id 1 有 2 和 3 作为孩子,id 4 有 8 和 5 作为孩子,依此类推。

我很难弄清楚这一点。 提前感谢您的帮助!

【问题讨论】:

  • 是否可以将此数据作为普通数组而不是对象使用?你从哪里得到它?

标签: php arrays loops multidimensional-array


【解决方案1】:

可以使用递归函数来扁平化这样的树结构。这是一个适用于您的树的示例。

function flatten($array_of_objects, $key = 0, &$result = []) {

    // iterate the objects in the array
    foreach ($array_of_objects as $object) {

        // add each object's id to the result
        $result[$key][] = $object->id;

        // if the object has children, make the recursive call on that array
        if (isset($object->children)) {
            flatten($object->children, $object->id, $result);
        }
    }
    return $result;
}

【讨论】:

  • 感谢您的帮助。完美运行。
【解决方案2】:
function wp_get_menu_array($current_menu) {

    $array_menu = wp_get_nav_menu_items($current_menu);
    $menu = array();
    foreach ($array_menu as $m1) {
        if (empty($m1->menu_item_parent)) {
            $menu[$m1->ID] = array();
            $menu[$m1->ID]['ID']             =   $m1->ID;
            $menu[$m1->ID]['title']          =   $m1->title;
            $menu[$m1->ID]['url']            =   $m1->url;
            $menu[$m1->ID]['children']       =   array();
            $childMenu = array();
            foreach ($array_menu as $m2) {
                if ($m2->menu_item_parent == $m1->ID) {
                    $childMenu[$m2->ID] = array();
                    $childMenu[$m2->ID]['ID']          =   $m2->ID;
                    $childMenu[$m2->ID]['title']       =   $m2->title;
                    $childMenu[$m2->ID]['url']         =   $m2->url;
                    $childMenu[$m2->ID]['children']       =   array();
                    $grandChildMenu = array();
                    foreach ($array_menu as $m3) {
                        if ($m3->menu_item_parent == $m2->ID) {
                            $grandChildMenu[$m3->ID] = array();
                            $grandChildMenu[$m3->ID]['ID']          =   $m3->ID;
                            $grandChildMenu[$m3->ID]['title']       =   $m3->title;
                            $grandChildMenu[$m3->ID]['url']         =   $m3->url;
                            $childMenu[$m3->menu_item_parent]['children'][$m3->ID] = $grandChildMenu[$m3->ID];
                        }
                    }
                    $menu[$m2->menu_item_parent]['children'][$m2->ID] = $childMenu[$m2->ID];
                }
            }
        }
    }
    return $menu;
}

【讨论】:

  • 一些解释以及代码将帮助任何查看您的答案的人。就像你如何解决问题的一个小总结。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多