Reputation: 539
why is php combining array when I do foreach. see below
If I enter the following code, I will get id1 id2
individually.
foreach($array as $value){
$id = $value->id;
echo $id;
}
now if I try to use the ids to do a query
foreach($array as $value){
$id = $value->id;
$result = $this->model->run_some_query($id);
var_dump($result);
}
for the above code. Since I am foreach looping not passing in an array of ids, I expect to get 2 sets of seperate array. array1 with result from id1, array2 with result from id2. but instead I get 1 array with result from both id merged together.
How to make it so the array is seperated.
Upvotes: 1
Views: 46
Reputation: 159
you can try this code on your loop statement
foreach($array as $value){
$id = $value->id;
$result[] = $this->model->run_some_query($id);
}
var_dump($result);
Upvotes: 0
Reputation: 940
Is $this->model->run_some_query($id)
returning an array reference, maybe? http://php.net/manual/en/language.references.php
Upvotes: 0
Reputation: 3144
You can get 2d array by doing that:
$result[id] = $this->model->run_some_query($id);
Upvotes: 1