I want to retrieve my data into a two-dimensional array that I can navigate through with a foreach loop, and be able to add new fields to the array afterwords. For example foreach($query_results as $qr) { $qr->brand_new_field = retrieve_other_data($qr->first_field, $qr->second_field); } Code (markup): I`m going around in circles with this but I can`t quite seem to get it right. Any suggestions?
foreach($query_results as &$qr) { $qr->brand_new_field = retrieve_other_data($qr->first_field, $qr->second_field); } PHP: Pass by reference, not value
I dont think you have to pass it by reference instead of value. More codes might help diagnose the problem.
Au contraire. Passing by reference instead of by value will fix his problem. Read through PHP's official documentation on the subject: http://au.php.net/manual/en/control-structures.foreach.php The problem is, when you simply use foreach to loop through, you're creating a temporary copy of the array value. In that sense, when you modify that value, you're only modifying the copy, and not the original Passing by reference allows you to loop through and make changes to the original array value.