[英]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']
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.