I have $row which outputs the below. Array 0 - 4 do not change per user but the last items in each array need to be added together.
array (
0 => '2',
1 => 'Joe',
2 => 'Bloggs',
3 => '[email protected]',
4 => '1,2',
5 => 1,
6 => 0,
7 => 1,
)array (
0 => '2',
1 => 'Joe',
2 => 'Bloggs',
3 => '[email protected]',
4 => '1,2',
5 => 0,
6 => 1,
7 => 1,
)array (
0 => '1',
1 => 'Jane',
2 => 'Doe',
3 => '[email protected]',
4 => '1,4',
5 => 1,
6 => 0,
7 => 1,
)array (
0 => '1',
1 => 'Jane',
2 => 'Doe',
3 => '[email protected]',
4 => '1,4',
5 => 0,
6 => 0,
7 => 0,
)
I need to combine them so they are like this:
Array
(
[0] => 2
[1] => Joe
[2] => Bloggs
[3] => [email protected]
[4] => 1,2
[5] => 1
[6] => 1
[7] => 2
)
Array
(
[0] => 1
[1] => Jane
[2] => Doe
[3] => [email protected]
[4] => 1,4
[5] => 1
[6] => 0
[7] => 1
)
My code so far is:
$combined = array();
foreach ($row as $item) {
if (!array_key_exists($item[0], $combined)) {
$combined[$item[0]] = $item;
} else {
array_push($combined, $item);
}
}
But this is not doing what I expect.
array_push, the element is inserted at the next available index. Your check is based on the index being the same as the user's id. If you want to go that way, then scrap the push and insert manually at the desired index (basically, yourifbranch contents should be in theelsebranch, and theifbranch should do the addition).