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.
ifconditional, you'll want to doif (!isset($idList[$value])) { $idList[$value] = []; }. The ensures that$idList[$value]is an array. Initializing$idListas 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'];).