I have an array in my PHP which I am iterating over twice, doing different things each time.
The array is called $data.
I have 2 foreach loops like this:
foreach($data as $item){
//doing stuff
}
foreach($data as $item){
//doing different stuff
}
I cant combine the 2 as this is how the application is but I want to move data into the $data array in the first iteration which will appear in the second iterations. The only way I could think of was below. e.g.
foreach($data as $item){
$data[]['new key']='my new data';
}
foreach($data as $item){
var_dump($item['new key']);//print 'my new data'
}
Is there a better way to do this?
Sounds like you want to pass array BY REFERENCE.
This means you are treating the array NOT like a typical array (a copy) but like a pointer to the
$dataarray.It is similar to how you might use an object (which is not a copy but an instance itself).
You can implement this with:
hope that helps