0

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
    ]
]
2
  • 1
    Perhaps you should share what kind of output you're trying to achieve. Right now it's hard to see what you're attempting at that line of code. Commented Feb 11, 2022 at 14:29
  • Please elaborate what you are trying to achieve here Commented Feb 11, 2022 at 14:30

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.