嵌套数组循环中未定义的数组键 php

Undefined array key in nested array loop php

我一直在尝试,但我不知道如何在没有此警告的情况下动态创建类似的东西,$response[$item[0]][$item[1]] += 1; 那条线是我在没有 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) {
        $response[$item[0]][$item[1]] += 1;
    }
    
    print_r($response);

这就是我的输出

➜ /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
        )

我想实现这个输出,我的输出有点像,但是有 php 警告,我只想修复输出块中未定义的数组键

[
    'black' =>  [
        'PP' => 1,
        'M' => 1,
        'G' => 1,
        'GG' => 2
    ],
    'white' => [
        'PP'=> 1,
        'G' => 1
    ],
    'red' => [
        'M' => 1
    ],
    'blue' => [
        'XG' => 3,
        'P' => 1
    ]
]
<?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);

您可以查看 isset 使用情况。

问题出在颜色的每个索引的第一次迭代中,它们不存在,需要先创建。