首页 > 解决方案 > 我需要显示数组的数组

问题描述

我正在开发一个 CMS,它使用 yaml 文件来操作主题。我的问题是我需要列出菜单的子菜单。例如,我访问 Header 菜单,我可以在其中更改其内容,但在该 Header 有子菜单时,我只想列出它们。

我有一项搜索服务

既然数据是一个数组,那么如何在屏幕上打印呢?

public function query() {

    /* @var $request Request */
    $request = app('request');

    /* @var $website Site */
    $website = $request->route('website');
    $menu = $request->route('submenu');

    // Carega configurações do site
    $this->service->loadWebsite($website->slug);
    /*
    - title: menu1
      submenu:
       - title: submenu 1.1
       - title: submenu 1.2
    - title: menu2
      submenu:
       - title: submenu 2.1
    */


    /*
    - title: menu1
    - title: - submenu 1.1
    - title: - submenu 1.2
    - title: menu2
    - title: - submenu 2.1
    */
    // puxa os menus da configuração do site
    $menus = $this->service->getWebsiteConfig($website->slug, 'menu.' . $menu . '.menu');

    dd($menus);
    return $menus;
}

服务

 protected function toWebisteMenuItemCollection(string $menu, array $rows) {
    return collect($rows)->map(function (array $data, string $key) use ($menu) {
        $data['id'] = $menu . '.' . $key;
        if(isset($data['submenu']) && is_array($data['submenu'])) {
            $data['submenu'] = $this->toWebisteMenuItemCollection($data['id'], $data['submenu']);
        }

        return new WebsiteMenuItem($data);
    });
}

dd($菜单)

#attributes: array:3 [
    "title" => "Consórcio"
    "submenu" => Collection {#647
      #items: array:3 [
        0 => WebsiteMenuItem {#642
          #keyType: "string"
          #fillable: array:7 [
            0 => "id"
            1 => "title"
            2 => "label"
            3 => "imagem"
            4 => "website_image"
            5 => "icons"
            6 => "submenu"
          ]
          #connection: null
          #table: null
          #primaryKey: "id"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: false
          +wasRecentlyCreated: false
          #attributes: array:2 [
            "title" => "Planos de Consórcio"
            "id" => "header_submenu_menu.2.0"
          ]
          #original: []
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [
            0 => "*"
          ]
        }
        1 => WebsiteMenuItem {#645
          #keyType: "string"
          #fillable: array:7 [
            0 => "id"
            1 => "title"
            2 => "label"
            3 => "imagem"
            4 => "website_image"
            5 => "icons"
            6 => "submenu"
          ]
          #connection: null
          #table: null
          #primaryKey: "id"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: false
          +wasRecentlyCreated: false
          #attributes: array:2 [
            "title" => "Portal do Consorciado"
            "id" => "header_submenu_menu.2.1"
          ]
          #original: []
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [
            0 => "*"
          ]
        }
        2 => WebsiteMenuItem {#646
          #keyType: "string"
          #fillable: array:7 [
            0 => "id"
            1 => "title"
            2 => "label"
            3 => "imagem"
            4 => "website_image"
            5 => "icons"
            6 => "submenu"
          ]
          #connection: null
          #table: null
          #primaryKey: "id"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: false
          +wasRecentlyCreated: false
          #attributes: array:2 [
            "title" => "Como Funciona o Consórcio"
            "id" => "header_submenu_menu.2.2"
          ]
          #original: []
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [
            0 => "*"
          ]
        }
      ]
    }

标签: phplaravelyaml

解决方案


var_dump($array_youd_like_to_print) 将为您提供数组的所有内容(甚至嵌套)。

如果它解决了您的问题,您可以按原样使用它,或者使用它来查找您正在寻找的值在哪里(以及如何在数组中访问它)并打印出来。


推荐阅读