I have an array of people that looks like this:
$people =
Array
(
[0] => Array
(
[email] => NameSurname@example.com
[name] => Name Surname
[count] => 0
)
[1] => Array
(
[email] => Name2Surname@example.com
[name] => Name2 Surname
[count] => 0
)
)
And I have an array which is the result of a MySQL query that looks like this:
$query=
Array
(
[0] => Array
(
[email] => NameSurname@example.com
[name] => Name Surname
)
[1] => Array
(
[email] => Name2Surname@example.com
[name] => Name2 Surname
)
[2] => Array
(
[email] => NameSurname@example.com
[name] => Name Surname
)
)
For each e-mail address in $people, I’d like $people['count'] to equal how many times that e-mail address occurs in $query.
I’ve tried loads of ways to do this, and I’m not quite getting the desired result.
For the avoidance of doubt, my end result based on the example above should look like:
$people =
Array
(
[0] => Array
(
[email] => NameSurname@example.com
[name] => Name Surname
[count] => 2
)
[1] => Array
(
[email] => Name2Surname@example.com
[name] => Name2 Surname
[count] => 1
)
)
UPD: One more solution using
array_map,array_reduceand niceternaryoperator. Its slower than one withforeach, but twice as compact and professional.It is slower (in terms of performance) bec. of function calls overhead, but on “small” amount of iterations this drop is negligible. It will update
$peoplearray without reassigning it bec. we transmitted each&$manby reference.