如何在 Laravel 中打印带有方括号而不是大括号的数组?
How to print arrays with square brackets instead of curly brackets in Laravel?
我的 laravel 控制器中有这段代码,它打印如下数组:Iitems 是一个集合。
$i = [];
foreach($items as $item) {
$i['grid'][$item->color]['color'] = $item->color;
$item_list['grid'][$item->color]['size_grid'][$item->size_no] = [
'size_no' => $item->size_no,
'size_description' => $item->size_desc,
'on_hand' => $item->on_hand,
];
}
输出:
"grid": {
"VELBL": {
"color": "VELBL",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
}
}
},
"BLUAS": {
"color": "BLUAS",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
},
"MHGR": {
"color": "MHGR",
"size_grid": {
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
}
}
这会用大括号打印结果,但我需要用方括号打印结果。我怎样才能做到这一点?如果我更改我的代码,它不会打印所有必要的数据,我如何打印这组带有方括号的相同数据,如下所示:
预期输出:
"grid": [
"VELBL": {
"color": "VELBL",
"size_grid": [
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
}
]
},
"BLUAS": {
"color": "BLUAS",
"size_grid": [
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
]
},
"MHGR": {
"color": "MHGR",
"size_grid": [
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
]
}
]
拜托,有人在这里帮助我,因为我是这项技术的新手。提前致谢。
更新:
通过添加这行代码,现在打印带有方括号的网格数组,但其中的数组 size_grid 不带有方括号,我如何打印 size_grid 数组也带有方括号?
$item_list['grid'] = array_values($item_list['grid']);
输出:
"grid": [
{
"color": "VELBL",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
}
}
},
{
"color": "BLUAS",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
},
{
"color": "MHGR",
"size_grid": {
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
}
]
要在网格中获得方括号内的结果,简单的解决方案如下:
// your existing code
$i = [];
foreach($items as $item) {
$i['grid'][$item->color]['color'] = $item->color;
$item_list['grid'][$item->color]['size_grid'][$item->size_no] = [
'size_no' => $item->size_no,
'size_description' => $item->size_desc,
'on_hand' => $item->on_hand,
];
}
// https://www.php.net/array_values
$item_list['grid'] = array_values($item_list['grid']);
更新:如果您希望size_grid也为数字,您可以执行以下操作:
// your existing code
foreach($items as $item) {
...
// this line is updated, removal of "$item->size_no"
$item_list['grid'][$item->color]['size_grid'][] = [
'size_no' => $item->size_no,
'size_description' => $item->size_desc,
'on_hand' => $item->on_hand,
];
}
// https://www.php.net/array_values
$item_list['grid'] = array_values($item_list['grid']);
或
...
// https://www.php.net/array_values
$item_list['grid'] = array_values($item_list['grid']);
$item_list['grid'] = array_map(
function ($item) {
$item['size_grid'] = array_values($item['size_grid']);
return $item;
},
$item_list['grid']
);
我的 laravel 控制器中有这段代码,它打印如下数组:Iitems 是一个集合。
$i = [];
foreach($items as $item) {
$i['grid'][$item->color]['color'] = $item->color;
$item_list['grid'][$item->color]['size_grid'][$item->size_no] = [
'size_no' => $item->size_no,
'size_description' => $item->size_desc,
'on_hand' => $item->on_hand,
];
}
输出:
"grid": {
"VELBL": {
"color": "VELBL",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
}
}
},
"BLUAS": {
"color": "BLUAS",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
},
"MHGR": {
"color": "MHGR",
"size_grid": {
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
}
}
这会用大括号打印结果,但我需要用方括号打印结果。我怎样才能做到这一点?如果我更改我的代码,它不会打印所有必要的数据,我如何打印这组带有方括号的相同数据,如下所示:
预期输出:
"grid": [
"VELBL": {
"color": "VELBL",
"size_grid": [
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
}
]
},
"BLUAS": {
"color": "BLUAS",
"size_grid": [
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
]
},
"MHGR": {
"color": "MHGR",
"size_grid": [
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
]
}
]
拜托,有人在这里帮助我,因为我是这项技术的新手。提前致谢。
更新: 通过添加这行代码,现在打印带有方括号的网格数组,但其中的数组 size_grid 不带有方括号,我如何打印 size_grid 数组也带有方括号?
$item_list['grid'] = array_values($item_list['grid']);
输出:
"grid": [
{
"color": "VELBL",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
}
}
},
{
"color": "BLUAS",
"size_grid": {
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
},
{
"color": "MHGR",
"size_grid": {
"1": {
"size_no": 1,
"size_description": "X SMALL",
"on_hand": "0"
},
"2": {
"size_no": 2,
"size_description": "SMALL",
"on_hand": "0"
},
"3": {
"size_no": 3,
"size_description": "MEDIUM",
"on_hand": "0"
},
"4": {
"size_no": 4,
"size_description": "LARGE",
"on_hand": "0"
},
"5": {
"size_no": 5,
"size_description": "X LARGE",
"on_hand": "0"
}
}
}
]
要在网格中获得方括号内的结果,简单的解决方案如下:
// your existing code
$i = [];
foreach($items as $item) {
$i['grid'][$item->color]['color'] = $item->color;
$item_list['grid'][$item->color]['size_grid'][$item->size_no] = [
'size_no' => $item->size_no,
'size_description' => $item->size_desc,
'on_hand' => $item->on_hand,
];
}
// https://www.php.net/array_values
$item_list['grid'] = array_values($item_list['grid']);
更新:如果您希望size_grid也为数字,您可以执行以下操作:
// your existing code
foreach($items as $item) {
...
// this line is updated, removal of "$item->size_no"
$item_list['grid'][$item->color]['size_grid'][] = [
'size_no' => $item->size_no,
'size_description' => $item->size_desc,
'on_hand' => $item->on_hand,
];
}
// https://www.php.net/array_values
$item_list['grid'] = array_values($item_list['grid']);
或
...
// https://www.php.net/array_values
$item_list['grid'] = array_values($item_list['grid']);
$item_list['grid'] = array_map(
function ($item) {
$item['size_grid'] = array_values($item['size_grid']);
return $item;
},
$item_list['grid']
);