RE: [PHP] Dispalying Arrays - Again

From: Date: Tue, 15 Apr 2003 11:38:22 +0000
Subject: RE: [PHP] Dispalying Arrays - Again
Groups: php.general 
Request: Send a blank email to php-general+get-143915@lists.php.net to get a copy of this message
> -----Original Message----- > From: Marcus Rasmussen [mailto:mr@gate58.dk] > Sent: 15 April 2003 11:38 > > I sure hope the array_shift function accesses the array in > the right order (it does.) What is the point if it didn't? Well, it's "right" in the sense that it accesses in the order they are stored in the array -- but this isn't necessarily the order that you want them in, and so is not "right" in that sense. In fact, going over both our posts very carefully, I don't think either of us got it right in terms of the Vernon's original problem, as I will show below! In any case, if array_shift() gives you the right order, so will foreach(), since they both address the array in its internal ordering, not the sorting order of the keys. And I know which I'd rather choose (unless I had a specific reason for destroying the array as I accessed it ;). > Array_shift does destroy the array, bot one could make a copy > if one needed it later. Well, but that's terribly inefficient! > Sorry to say, but Mike, you'r code does not work. > The keys in the 'user' and in the 'distance' are not the > same, so you can't use the key from the 'user' to get the 'distance'. But the in the problem as originally posted they should have been the same, since the arrays were created thus: > do { > $z['username'][] = $row_rsUSERS['username']; > $z['distance'][] = $totaldist; > > } while ($row_rsUSERS = mysql_fetch_assoc($rsUSERS)); Now, here's the first screwy thing -- if the arrays were created using the above code, why has Vernon reported that he gets a display "something like" this: for $z['username'] 0 = vernman 1 = Marc 2 = rich69 3 = lata7 for $z['distance'] 0 = 0.0 4 = 14.9 9 = 15.0 1 = 67.6 So, to Vernon: is that only "something like" what you got echoed, or is it EXACTLY what you got echoed; if it's the latter, I don't see how the code you've shown us could possibly have created it...!! Anyway, notwithstanding that, it would probably be better to propagate your own incrementing key, and use it explicitly for both subarrays, so that we can be *sure* that the keys match -- something like: $k = 0; do { $z['username'][$k] = $row_rsUSERS['username']; $z['distance'][$k++] = $totaldist; } while ($row_rsUSERS = mysql_fetch_assoc($rsUSERS)); Right, so assuming that your subarrays actually do have matching keys, this would give us (say): $z['user'] = array(0=>'vernman', 1=>'Marc', 2=>'rich96', 3=>'lata7'); $z['distance'] = array(0 => 0.0, 1 => 67.6, 2 => 15.0, 3 => 14.9, ); Sorting the distance array, preserving key-correspondence (which is what asort() does), would give: $z['user'] = array(0=>'vernman', 1=>'Marc', 2=>'rich96', 3=>'lata7'); $z['distance'] = array(0 => 0.0, 3 => 14.9, 2 => 15.0, 1 => 67.6); Now we get to the bit where I don't think anybody's addressed the underlying problem. Notice how only the ['distance'] subarray has been sorted -- this is presumably because Vernon's stated goal is to output the values in order of increasing distance. This means we have to do the output in the order of the sorted distances (which no-one has yet pointed out, as far as I have seen), and use the keys to correspond back to the user name. This gives us: foreach($z['distance'] as $k => $v){ echo "{$z['user'[$k]}, $v<br>"; } and hopefully we have now fully solved the problem. Unless, of course, someone knows better...? Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@lmu.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#143915) next »