0

here is what I'm trying to do. I'm retrieving information from a database via array. What is happening is the information from the previous array is going into the next array.

Here is the code:

$i = 0;
foreach ($array_name as $key => test_name) {
 $id = $test_name['id']

 foreach ($test_name['id] as $key => $test_id {
     $data = ModelClass::Information($test_id);
     $array_name[$i]['new_infroamtion'] = $data'
  }
}

So right now based on the code data from the table is correctly going into the first array, however, information based from the first array is going into the second array..

Let me know if you need anymore information.

Thank you

5
  • Is your actual code missing the ' in $test_name['id]? Commented Mar 4, 2016 at 21:00
  • 1
    and a closing bracket ')' on the second foreach loop Commented Mar 4, 2016 at 21:02
  • Actually that was a typo on my part.. for here. Commented Mar 4, 2016 at 21:04
  • What is first array, what is second array, example please. Commented Mar 4, 2016 at 21:04
  • I can't seem to modify this question for some reason. Commented Mar 4, 2016 at 21:07

2 Answers 2

1

You are using $array_name while you are iterating through $array_name. This is valid code if you want to do this, but I don't think you do. You need to change the second $array_name to something else.

$i = 0;
foreach (**$array_name** as $key => test_name) {
   $id = $test_name['id']

   foreach ($test_name['id'] as $key => $test_id {
     $data = ModelClass::Information($test_id);
     **$array_name**[$i]['new_infroamtion'] = $data
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I did find a solution. What I had to do was add the following

$s = array()

Then in the for loop, I added the following code:

foreach ($test_name['id] as $key => $test_id {
   $data = ModelClass::Information($test_id);
   $s[] = $data
   $array_name[$i]['new_infroamtion'] = $s'
  }

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.