我需要显示一个数组的数组

时间:2019-01-16 21:04:14

标签: php laravel yaml

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

我有可以搜索的服务

由于数据是数组,如何在屏幕上打印?

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($ menu)

#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 => "*"
          ]
        }
      ]
    }

1 个答案:

答案 0 :(得分:0)

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

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