1

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.

0

3 Answers 3

1

You can get 2d array by doing that:

$result[id] = $this->model->run_some_query($id);
Sign up to request clarification or add additional context in comments.

Comments

0

Is $this->model->run_some_query($id) returning an array reference, maybe? http://php.net/manual/en/language.references.php

Comments

0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.