PHP - 用对象反转数组

时间:2017-10-30 15:15:41

标签: php

我有一个递归函数,我从WP得到的响应中排序菜单元素。这是功能:

private function menuBuilder($menuItems, $parentId = 0)
{
    $hiddenAttributes = \Config::get('middleton.wp.menuHiddenAttributes');
    $res = [];

    foreach ($menuItems as &$item) {
        $itemParentId = $item->meta->_menu_item_menu_item_parent;

        if ($itemParentId == $parentId) {
            $children = $this->menuBuilder($menuItems, $item->ID);

            if ($children) {
                $item['submenu'] = $children;
            }

            if ($item->post_title == '') {
                $item = $this->modifyPostItem($item, $hiddenAttributes);
            }
            else {
              $item->makeHidden($hiddenAttributes)->toArray();
            }

            $res[$item['ID']] =  $item;
            unset($item);
        }
    }

    return $res;
}

我从索引函数调用此函数:

public function index()
{
    $menus = Menu::all();
    $menu = Menu::slug('hovedmeny')->first();
    $res = $this->menuBuilder($menu->nav_items);

    return $res;
}

如果我做dd($res),在我返回之前,我会在邮递员中得到这样的结果:

array:2 [
  161 => Post {#342
    #table: "posts"
    #primaryKey: "ID"
    #dates: array:4 [
      0 => "post_date"
      1 => "post_date_gmt"
      2 => "post_modified"
      3 => "post_modified_gmt"
    ]
    #with: array:1 [
      0 => "meta"
    ]
    #fillable: array:7 [
      0 => "post_content"
      1 => "post_title"
      2 => "post_excerpt"
      3 => "post_type"
      4 => "to_ping"
      5 => "pinged"
      6 => "post_content_filtered"
    ]
    #appends: array:17 [
      0 => "title"
      1 => "slug"
      2 => "content"
      3 => "type"
      4 => "mime_type"
      5 => "url"
      6 => "author_id"
      7 => "parent_id"
      8 => "created_at"
      9 => "updated_at"
      10 => "excerpt"
      11 => "status"
      12 => "image"
      13 => "terms"
      14 => "main_category"
      15 => "keywords"
      16 => "keywords_str"
    ]
    #connection: "wordpress"
    #keyType: "int"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:23 [
      "ID" => 161
      "post_author" => 1
      "post_date" => "2017-08-29 11:37:12"
      "post_date_gmt" => "2017-08-29 11:37:12"
      "post_content" => ""
      "post_title" => "Hjem"
      "post_excerpt" => ""
      "post_status" => "publish"
      "comment_status" => "closed"
      "ping_status" => "closed"
      "post_password" => ""
      "post_name" => "hjem"
      "to_ping" => ""
      "pinged" => ""
      "post_modified" => "2017-09-12 12:22:49"
      "post_modified_gmt" => "2017-09-12 12:22:49"
      "post_content_filtered" => ""
      "post_parent" => 0
      "guid" => "http://ytf.app/?p=161"
      "menu_order" => 1
      "post_type" => "nav_menu_item"
      "post_mime_type" => ""
      "comment_count" => 0
    ]
    #original: array:25 [
      "ID" => 161
      "post_author" => 1
      "post_date" => "2017-08-29 11:37:12"
      "post_date_gmt" => "2017-08-29 11:37:12"
      "post_content" => ""
      "post_title" => "Hjem"
      "post_excerpt" => ""
      "post_status" => "publish"
      "comment_status" => "closed"
      "ping_status" => "closed"
      "post_password" => ""
      "post_name" => "hjem"
      "to_ping" => ""
      "pinged" => ""
      "post_modified" => "2017-09-12 12:22:49"
      "post_modified_gmt" => "2017-09-12 12:22:49"
      "post_content_filtered" => ""
      "post_parent" => 0
      "guid" => "http://ytf.app/?p=161"
      "menu_order" => 1
      "post_type" => "nav_menu_item"
      "post_mime_type" => ""
      "comment_count" => 0
      "pivot_term_taxonomy_id" => 49
      "pivot_object_id" => 161
    ]
    #relations: array:3 [
      "pivot" => Pivot {#341
        #parent: Menu {#288
          +id: null
          +name: null
          +type: null
          +cmsId: null
          #taxonomy: "nav_menu"
          #with: array:2 [
            0 => "term"
            1 => "nav_items"
          ]
          #table: "term_taxonomy"
          #primaryKey: "term_taxonomy_id"
          +timestamps: false
          #connection: "wordpress"
          #keyType: "int"
          #perPage: 15
          +incrementing: true
          #attributes: []
          #original: []
          #relations: []
          #hidden: []
          #visible: []
          #appends: []
          #fillable: []
          #guarded: array:1 [
            0 => "*"
          ]
          #dates: []
          #dateFormat: null
          #casts: []
          #touches: []
          #observables: []
          #morphClass: null
          +exists: false
          +wasRecentlyCreated: false
        }
        #foreignKey: "term_taxonomy_id"
        #otherKey: "object_id"
        #guarded: []
        #connection: "wordpress"
        #table: "term_relationships"
        #primaryKey: "id"
        #keyType: "int"
        #perPage: 15
        +incrementing: true
        +timestamps: false
        #attributes: array:2 [
          "term_taxonomy_id" => 49
          "object_id" => 161
        ]
        #original: array:2 [
          "term_taxonomy_id" => 49
          "object_id" => 161
        ]
        #relations: []
        #hidden: []
        #visible: []
        #appends: []
        #fillable: []
        #dates: []
        #dateFormat: null
        #casts: []
        #touches: []
        #observables: []
        #with: []
        #morphClass: null
        +exists: true
        +wasRecentlyCreated: false
      }
      "meta" => PostMetaCollection {#363
        #changedKeys: []
        #items: array:8 [
          0 => PostMeta {#366
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 187
              "post_id" => 161
              "meta_key" => "_menu_item_type"
              "meta_value" => "custom"
            ]
            #original: array:4 [
              "meta_id" => 187
              "post_id" => 161
              "meta_key" => "_menu_item_type"
              "meta_value" => "custom"
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          1 => PostMeta {#367
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 188
              "post_id" => 161
              "meta_key" => "_menu_item_menu_item_parent"
              "meta_value" => "0"
            ]
            #original: array:4 [
              "meta_id" => 188
              "post_id" => 161
              "meta_key" => "_menu_item_menu_item_parent"
              "meta_value" => "0"
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          2 => PostMeta {#368
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 189
              "post_id" => 161
              "meta_key" => "_menu_item_object_id"
              "meta_value" => "161"
            ]
            #original: array:4 [
              "meta_id" => 189
              "post_id" => 161
              "meta_key" => "_menu_item_object_id"
              "meta_value" => "161"
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          3 => PostMeta {#369
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 190
              "post_id" => 161
              "meta_key" => "_menu_item_object"
              "meta_value" => "custom"
            ]
            #original: array:4 [
              "meta_id" => 190
              "post_id" => 161
              "meta_key" => "_menu_item_object"
              "meta_value" => "custom"
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          4 => PostMeta {#370
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 191
              "post_id" => 161
              "meta_key" => "_menu_item_target"
              "meta_value" => ""
            ]
            #original: array:4 [
              "meta_id" => 191
              "post_id" => 161
              "meta_key" => "_menu_item_target"
              "meta_value" => ""
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          5 => PostMeta {#371
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 192
              "post_id" => 161
              "meta_key" => "_menu_item_classes"
              "meta_value" => "a:1:{i:0;s:0:"";}"
            ]
            #original: array:4 [
              "meta_id" => 192
              "post_id" => 161
              "meta_key" => "_menu_item_classes"
              "meta_value" => "a:1:{i:0;s:0:"";}"
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          6 => PostMeta {#372
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 193
              "post_id" => 161
              "meta_key" => "_menu_item_xfn"
              "meta_value" => ""
            ]
            #original: array:4 [
              "meta_id" => 193
              "post_id" => 161
              "meta_key" => "_menu_item_xfn"
              "meta_value" => ""
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
          7 => PostMeta {#373
            #table: "postmeta"
            #primaryKey: "meta_id"
            +timestamps: false
            #fillable: array:3 [
              0 => "meta_key"
              1 => "meta_value"
              2 => "post_id"
            ]
            #appends: array:1 [
              0 => "value"
            ]
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:4 [
              "meta_id" => 194
              "post_id" => 161
              "meta_key" => "_menu_item_url"
              "meta_value" => "/"
            ]
            #original: array:4 [
              "meta_id" => 194
              "post_id" => 161
              "meta_key" => "_menu_item_url"
              "meta_value" => "/"
            ]
            #relations: []
            #hidden: []
            #visible: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
        ]
      }
      "taxonomies" => Collection {#354
        #items: array:1 [
          0 => TermTaxonomy {#359
            #table: "term_taxonomy"
            #primaryKey: "term_taxonomy_id"
            #with: array:1 [
              0 => "term"
            ]
            +timestamps: false
            #connection: "wordpress"
            #keyType: "int"
            #perPage: 15
            +incrementing: true
            #attributes: array:6 [
              "term_taxonomy_id" => 49
              "term_id" => 49
              "taxonomy" => "nav_menu"
              "description" => ""
              "parent" => 0
              "count" => 2
            ]
            #original: array:8 [
              "term_taxonomy_id" => 49
              "term_id" => 49
              "taxonomy" => "nav_menu"
              "description" => ""
              "parent" => 0
              "count" => 2
              "pivot_object_id" => 161
              "pivot_term_taxonomy_id" => 49
            ]
            #relations: array:2 [
              "pivot" => Pivot {#360
                #parent: Post {#342}
                #foreignKey: "object_id"
                #otherKey: "term_taxonomy_id"
                #guarded: []
                #connection: "wordpress"
                #table: "term_relationships"
                #primaryKey: "id"
                #keyType: "int"
                #perPage: 15
                +incrementing: true
                +timestamps: false
                #attributes: array:2 [
                  "object_id" => 161
                  "term_taxonomy_id" => 49
                ]
                #original: array:2 [
                  "object_id" => 161
                  "term_taxonomy_id" => 49
                ]
                #relations: []
                #hidden: []
                #visible: []
                #appends: []
                #fillable: []
                #dates: []
                #dateFormat: null
                #casts: []
                #touches: []
                #observables: []
                #with: []
                #morphClass: null
                +exists: true
                +wasRecentlyCreated: false
              }
              "term" => Term {#353
                #table: "terms"
                #primaryKey: "term_id"
                +timestamps: false
                #connection: "wordpress"
                #keyType: "int"
                #perPage: 15
                +incrementing: true
                #attributes: array:4 [
                  "term_id" => 49
                  "name" => "hovedmeny"
                  "slug" => "hovedmeny"
                  "term_group" => 0
                ]
                #original: array:4 [
                  "term_id" => 49
                  "name" => "hovedmeny"
                  "slug" => "hovedmeny"
                  "term_group" => 0
                ]
                #relations: []
                #hidden: []
                #visible: []
                #appends: []
                #fillable: []
                #guarded: array:1 [
                  0 => "*"
                ]
                #dates: []
                #dateFormat: null
                #casts: []
                #touches: []
                #observables: []
                #with: []
                #morphClass: null
                +exists: true
                +wasRecentlyCreated: false
              }
            ]
            #hidden: []
            #visible: []
            #appends: []
            #fillable: []
            #guarded: array:1 [
              0 => "*"
            ]
            #dates: []
            #dateFormat: null
            #casts: []
            #touches: []
            #observables: []
            #morphClass: null
            +exists: true
            +wasRecentlyCreated: false
          }
        ]
      }
    ]
    #hidden: array:37 [
      0 => "author_id"
      1 => "comment_count"
      2 => "comment_status"
      3 => "content"
      4 => "created_at"
      5 => "excerpt"
      6 => "guid"
      7 => "image"
      8 => "keywords"
      9 => "keywords_str"
      10 => "menu_order"
      11 => "meta"
      12 => "mime_type"
      13 => "parent_id"
      14 => "pinged"
      15 => "ping_status"
      16 => "pivot"
      17 => "post_author"
      18 => "post_content"
      19 => "post_content_filtered"
      20 => "post_date"
      21 => "post_date_gmt"
      22 => "post_excerpt"
      23 => "post_mime_type"
      24 => "post_modified"
      25 => "post_modified_gmt"
      26 => "post_name"
      27 => "post_parent"
      28 => "post_password"
      29 => "post_status"
      30 => "post_title"
      31 => "post_type"
      32 => "taxonomies"
      33 => "terms"
      34 => "to_ping"
      35 => "type"
      36 => "updated_at"
    ]
    #visible: []
    #guarded: array:1 [
      0 => "*"
    ]
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #morphClass: null
    +exists: true
    +wasRecentlyCreated: false
  }
  2 => array:6 [
    "ID" => 2
    "title" => "Sample Page"
    "slug" => "sample-page"
    "url" => "/page/sample-page"
    "status" => "publish"
    "main_category" => "hovedmeny"
  ]
]

但是,当我从索引函数返回$res时,我得到的实际上是这样的:

{
    "2": {
        "ID": 2,
        "title": "Sample Page",
        "slug": "sample-page",
        "url": "/page/sample-page",
        "status": "publish",
        "main_category": "hovedmeny"
    },
    "161": {
        "ID": 161,
        "title": "Hjem",
        "slug": "hjem",
        "url": "http://ytf.app/?p=161",
        "status": "publish",
        "main_category": "hovedmeny"
    }
}

所以,当我从函数返回它们时,看起来对象的顺序是相反的,为什么是这样,我怎么能用反过来的呢?

0 个答案:

没有答案