J'ai ce morceau de code dans mon contrôleur laravel et il imprime un tableau comme ci-dessous : Iitems est une 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,
   ];
}

Sortie:

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

Cela imprime le résultat avec des accolades, mais j'ai besoin que cela soit imprimé avec des crochets. Comment puis-je atteindre cet objectif? Si je modifie mon code, il n'imprime pas toutes les données nécessaires, comment puis-je imprimer ce même ensemble de données avec des crochets comme ci-dessous :

Sortie attendue:

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

S'il vous plaît, quelqu'un, aidez-moi ici car je suis nouveau dans la technologie. Merci d'avance.

Mise à jour : En ajoutant cette ligne de code, cela imprime maintenant le tableau de grille avec des crochets, mais le tableau à l'intérieur, size_grid pas avec des crochets, comment puis-je imprimer le tableau size_grid également avec des crochets ?

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

Sortie:

"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"
              }
            }
          }
        ]
0
user13080158 9 oct. 2020 à 12:48

1 réponse

Meilleure réponse

Pour obtenir un résultat entre crochets dans la grille, la solution simple serait quelque chose comme ceci :

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

Mise à jour : si vous souhaitez que size_grid soit également numérique, vous pouvez procéder comme suit :

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

OU

...

// 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']
);
0
Eelke van den Bos 9 oct. 2020 à 11:22