
Originally Posted by
StarLion
You see that little & before $listing in the foreach of last code block? It's
pretty significant...
Now; what EXACTLY are you trying to do? Or, to put it in a phrase my programming brain can work on it (it's only 9 AM), answer the two questions:
What do you have BEFORE (Input)
What do you want AFTER (Output)
Thank you for your response. I'm sorry if I did not give an adequate explanation of what I am (successfully) doing. My question was aimed at finding out if there was a better way (or why what appears to be the right way isn't working for me).
Right at the start of this process I have an array called $listings (plural). It contains many sub-arrays (about 200) which represent records from a MySQL database. These sub-arrays all have the same structure. If I were to create them manually they'd look like this:
PHP Code:
listings[] = ('bus_id' => '1234', 'name' => 'name1', ......);
listings[] = ('bus_id' => '1235', 'name' => 'name2', ......);
and so on
They DO NOT include the KVPs 'dist' and 'unit'. In first of the three 'foreach' loops I am finding latlong co-ordinates from another table and calculating a distance, and I'm using the second 'foreach' to INSERT NEW KVPs into each sub-array when the appropriate record is found. They then look like this:
PHP Code:
listings[] = ('bus_id' => '1234', 'name' => 'name1', 'dist' => $dist1, 'unit' => $unit......);
listings[] = ('bus_id' => '1235', 'name' => 'name2', 'dist' => $dist2, 'unit' => $unit......);
and so on. At this point 'unit' defaults to miles.
Now I need the changed sub-array to be 'written' back to $listings. That's where I thought '&$value' would come in, but it didn't seem to work, so I resorted to saving the changed sub-arrays to a new 'wrapper' array ($newlistings), and then changed its name afterwards. (OK, so the way the PHP Manual describes how '&$value' works is that it allows me to work on the actual $value rather than a copy, but it achieves the same effect as what I've called 'writing back'. How you look at it is less important than what it achieves.)
Next I sort the order of the sub-arrays within the main array by the value of 'dist' within each sub-array. That works fine, so no more on that score. EXCEPT to say that this is where it fails when I try to use' &$value' above. I get "Undefined index: dist...' which tells me the added KVPs haven't been 'written back' (as I call it) to the original $listings. So long as I write my augmented sub-arrays to another wrapper ($newlistings in my code) all is well. The presence of '&$listings' in the first code sample in my original posting made no difference, so I should probably have left it our for clarity.
Finally in the third 'foreach' I go through the sub-arrays looking for low values of 'dist' and converting them from miles to yards, and changing the 'unit' to yards too. In this case the '&$value' seems to have the desired effect of allowing me to work on the actual listings rather than a copy, so I don't explicitly have to write them to another array.
PHP Code:
// Convert small distances to yards
foreach ($listings as &$listing) {
if (isset($listing['dist']) && $listing['dist'] < 0.5) {
$listing['dist'] = round($listing['dist']*1760,-1);
$listing['unit'] = 'yards';
}
// $newlistings[] = $listing; (not needed here)
}
// $listings = $newlistings; (not needed here)
As before, my question boils down very simply to: Why does '&$value' appear to work when I'm just altering existing KVPs, but NOT when I'm adding additional ones ?
If you'd like a summary, it's like this:
Case 1: (&$value does not work)
Before: An array of arrays
After: An array of arrays which (all) contain some extra KVPs
Case2: (&$value works)
Before: An array of arrays
After: An array of arrays which contain the same KVPs, two of which have been altered.
I hope this is understandable. It has become very long.
Bookmarks