-2

i'm missing something simple here trying to add a new key/value pair and then add to the value of that pair as this loop goes on. this throws an undefined index error:

$someAssocArray = [2] => 'flarn'
                  [3] => 'turlb'
$someOtherAssocArray = [0] => 'id' => '11'
                         'name' => 'flarn'
                       [1] => 'id' => '22'
                         'name' => 'turlb'
                       [2] => 'id' => '33'
                         'name' => 'turlb'
                       [3] => 'id' => '44'
                         'name' => 'flarn'
$idList = [];
foreach($someAssocArray as $key=>$value) {
  foreach($someOtherAssocArray as $item) {
    if($item['name'] === $value) {
      $idList[$value] += $item['id'];
    }
  }
}

the end result of idList should look like this:

$idList = [ "flarn" => "11,44"
            "turlb" => "22,33" ]

so please tell me what i'm missing so i can facepalm and move on.

WhiteRau
  • 818
  • 14
  • 32
  • inside your `if` conditional, you'll want to do `if (!isset($idList[$value])) { $idList[$value] = []; }`. The ensures that `$idList[$value]` is an array. Initializing `$idList` as an empty array before your loops is a good start, but then your script tries to do something like `$idList['test'] += 11;` , but at this point `$idList['test']` is undefined. It's also not clear if you're intending to add (11 + 44) and set that value for 'flarn', or if you're intending on concatenating the value (`$idList[$value] = $idList[$value].','.$item['id'];`). – WOUNDEDStevenJones Mar 30 '21 at 16:30
  • Please tell us which line is it crashing at. – Lajos Arpad Mar 31 '21 at 10:10

1 Answers1

1

[Edit] OK I just re-read that question and I might be misunderstanding. Is the desired output of 11,44 supposed to represent the sum of the values? Or a list of them?

This code will generate a warning if $idList[$value] doesn't exist:

$idList[$value] += $item['id'];

This is happening because it has no value yet for the first time you're incrementing. You can avoid this issue by initializing the value to zero for that first time when it doesn't exist:

If you want a sum of the values:

if($item['name'] === $value) {
    $idList[$value] ??= 0; // initialize to zero
    $idList[$value] += $item['id']; // add each value to the previous
}

If you want a list of the values:

if($item['name'] === $value) {
    $idList[$value] ??= []; // initialize to an empty array
    $idList[$value][] = $item['id']; // append each element to the list
}

(Then you can use implode() to output a comma-separated string if desired.)

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • What's `??=`? I've never encountered this syntax before. – El_Vanja Mar 30 '21 at 16:34
  • `??=` is the null coalescing assignment operator added in PHP 7.4 - https://stackoverflow.com/questions/59102708/what-is-null-coalescing-assignment-operator-in-php-7-4 – Qirel Mar 30 '21 at 16:35
  • https://stackoverflow.com/questions/64441592/double-question-mark-and-an-equal-sign-what-does-that-operator-do – WOUNDEDStevenJones Mar 30 '21 at 16:36
  • Silly me. I completely ignored the [valid output in 3v4l](https://3v4l.org/K7lYN) and focused solely on the syntax error it was reporting. – El_Vanja Mar 30 '21 at 16:40
  • alright! while i was waiting, i'd assessed that the array needed initializing, but i was doing it wrong there too. your answer hit this on the head AND i learned about the `??=` operator. AND this also made what i needed to do next much easier as well, because i don't have to explode the result, i can just pass it straight in. triple rainbow! :D thank you. – WhiteRau Mar 30 '21 at 17:27