【问题标题】:I need to show array of an array我需要显示数组的数组
【发布时间】:2019-01-16 21:04:14
【问题描述】:

我正在开发一个 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($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 => "*"
          ]
        }
      ]
    }

【问题讨论】:

    标签: php laravel yaml


    【解决方案1】:

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

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

    【讨论】:

    • dd ($ menu) 将所有数据返回到子菜单,但我想获取每个菜单索引并检查它是否有子菜单,如果有,只需列出标题它。
    • 您可以使用 foreach 循环。例如: foreach ($menuItems as $menuItem) { // 在这里对 $menuItem 做一些事情(也许检查它是否有你想要的值?) }
    • 嗯,我这里试试
    • 但是yaml的数据不是对象,只是一个数组。
    • 您可以使用 yaml_parse() 或其他 yaml php 函数将数据从 yaml 解析为 php 对象。
    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2016-07-21
    • 2017-07-13
    相关资源
    最近更新 更多