如何在 Laravel 中打印带有方括号而不是大括号的数组? [重复]

问题描述 投票:0回答:1

我的 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"
              }
            }
          }
        ]
php arrays json curly-braces square-bracket
1个回答
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']
);
© www.soinside.com 2019 - 2024. All rights reserved.