Home > OS >  Undefined array key in nested array loop php
Undefined array key in nested array loop php

Time:02-12

I have been trying but i don't know how i can create something like that dynamically without this warnings, $response[$item[0]][$item[1]] = 1; that line is where i struggling to run without php warnings of undefined, thats my code

   $data = [
        'blue-XG',
        'white-PP',
        'blue-XG',
        'black-PP',
        'black-M',
        'white-G',
        'black-G',
        'vermelho-M',
        'black-GG',
        'blue-P',
        'black-GG',
        'blue-XG',
    ];
    sort($data);
    
    $formatted = array_map(fn($c) => explode('-', $c), $data);
    
    $response = [];
    foreach ($formatted as $item) {
        $response[$item[0]][$item[1]]  = 1;
    }
    
    print_r($response);

and that is my output

➜ /tmp php product.php
PHP Warning:  Undefined array key "black" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "G" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "GG" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "M" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "PP" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "blue" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "P" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "XG" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "vermelho" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "M" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "white" in /tmp/product.php on line 34
PHP Warning:
PHP Warning:  Undefined array key "black" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "G" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "GG" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "M" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "PP" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "blue" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "P" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "XG" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "vermelho" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "M" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "white" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "G" in /tmp/product.php on line 34
PHP Warning:  Undefined array key "PP" in /tmp/product.php on line 34
Array
(
    [black] => Array
        (
            [G] => 1
            [GG] => 2
            [M] => 1
            [PP] => 1
        )

    [blue] => Array
        (
            [P] => 1
            [XG] => 3
        )

    [red] => Array
        (
            [M] => 1
        )

    [white] => Array
        (
            [G] => 1
            [PP] => 1
        )

I want to achieve this output, my output kind of works, but with php warnings, i just want to fix the undefined array keys in the output block

[
    'black' =>  [
        'PP' => 1,
        'M' => 1,
        'G' => 1,
        'GG' => 2
    ],
    'white' => [
        'PP'=> 1,
        'G' => 1
    ],
    'red' => [
        'M' => 1
    ],
    'blue' => [
        'XG' => 3,
        'P' => 1
    ]
]

CodePudding user response:

<?php

$data = [
    'blue-XG',
    'white-PP',
    'blue-XG',
    'black-PP',
    'black-M',
    'white-G',
    'black-G',
    'vermelho-M',
    'black-GG',
    'blue-P',
    'black-GG',
    'blue-XG',
];
sort($data);

$formatted = array_map(fn($c) => explode('-', $c), $data);

$response = [];
foreach ($formatted as $item) {
    // if $response does not have $item[0] key, create it
    if (!isset($response[$item[0]])) {
        $response[$item[0]] = [];
    }

    // if $response does not have $item[0][$item1] value, create it and add 0 value
    if (!isset($response[$item[0]][$item[1]])) {
        $response[$item[0]][$item[1]] = 0;
    }

      $response[$item[0]][$item[1]];
}

print_r($response);

you can check isset on usage.

The problem is on first iteration of each index of the colours, they do not exist and needs to be created first.

  • Related