phparraysjsoncurly-bracessquare-bracket

How to print arrays with square brackets instead of curly brackets in Laravel?


I have this piece of code in my laravel controller and it prints an array as below: Iitems is a collection.

$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,
   ];
}

Output:

"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"
              }
            }
          }
        }

This prints the result with curly brackets, but I need this to be print with square brackets. How can I achieve this? If I change my code it's not printing all necessary data, how can I print this same set of data with square brackets as below:

Expected output:

"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"
              }
            ]
          }
        ]

Please, someone, help me here as am new to the technology. Thanks in advance.

Update: By adding this line of code, now this prints the grid array with square brackets, but the array inside it, size_grid not with square brackets, how can I print size_grid array also with square brackets?

$item_list['grid'] = array_values($item_list['grid']);

Output:

"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"
              }
            }
          }
        ]

Solution

  • To obtain a square bracketed result within grid, the simple solution would be something like this:

    // 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']);
    

    Update: if you want size_grid to be numeric as well, you can do the following:

    // 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']);
    

    OR

    ...
    
    // 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']
    );