Reputation: 21
I have this funny feeling like this is going to be something simple that my tired eyes have missed.
Given the following array $query
:
Array ( [report_id] => 19 [facebook_id] => Array ( [0] => 12345678 [1] => 12345678 [2] => 12345678 [3] => 12345678 [4] => 12345678 ) )
(Never mind the values of the [facebook_id]
array, I just put that in there for the example)
I need to iterate over and pass each value to a function. I'm using the following foreach loop, and when I test how many times it's looping it goes through double.
foreach ($query as $value) {
$report_id = $query['report_id'];
$looper = $query['facebook_id'];
$i = 0;
foreach ($looper as $inner) {
if ($i == 0) {
$facebook_id_list = $inner;
} else {
$facebook_id_list = $facebook_id_list.'|'.$inner;
}
$i++;
}
}
The output when I echo everything back is:
19
12345678|12345678|12345678|12345678|12345678
19
12345678|12345678|12345678|12345678|12345678
I'm eventually going to explode the $facebook_id_list
in my function, that's why I'm concatenating the multi-dimensional array into the variable.
What am I missing?
Upvotes: 0
Views: 2517
Reputation: 781068
Your loop can be replaced with:
$report_id = $query['report_id'];
$facebook_id_list = implode('|', $query['facebook_id']);
No loop, no looping twice.
Upvotes: 1
Reputation: 16035
It is looping twice because you have 2 elements in $query and you're setting $report_id and $looper inside the loop, which seems kind of pointless as you're not using $value anywhere.
Good luck!
Upvotes: 0