Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined array key in nested array loop php

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

<?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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading