php - 我需要显示数组的数组
问题描述
我正在开发一个 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 => "*"
]
}
]
}
解决方案
var_dump($array_youd_like_to_print) 将为您提供数组的所有内容(甚至嵌套)。
如果它解决了您的问题,您可以按原样使用它,或者使用它来查找您正在寻找的值在哪里(以及如何在数组中访问它)并打印出来。
推荐阅读
- python - 创建一个圆角数组以不考虑欧几里德距离python
- ios - 比较调用树选项在仪器中被禁用
- python-3.x - Python-Selenium 脚本同时打开 Edge 和 IE 而不是仅打开 Edge
- android - 是否可以更改触摸事件的调用频率?
- android - Firebase 更新后 Gradle 同步失败
- gcc - 为什么动态链接需要在 Linux 平台上存在带有 GCC 的 SO 文件?
- abap - SAP SALV 总是在列标题中显示长文本
- symfony - 可空列的数据库架构不同步
- python - 在 Visual Studio Code 中设置选项卡空间样式...根据项目类型
- php - 一次查询中的对话用户列表