RE: [PHP] Dispalying Arrays - Again
| From: | Marcus Rasmussen | Date: | Tue, 15 Apr 2003 10:37:57 +0000 |
| Subject: | RE: [PHP] Dispalying Arrays - Again | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143900@lists.php.net to get a copy of this message | ||
I sure hope the array_shift function accesses the array in the right order (it does.) What is the
point if it didn't?
Array_shift does destroy the array, bot one could make a copy if one needed it later.
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'.
If one realy didn't want to use array_shift then this might do instead: (not sure if it's
faster or whatever.)
$z['user'] = array('vernman', 'Marc', 'rich96',
'lata7');
$z['distance'] = array(0 => 0.0, 4 => 14.9, 9 => 15.0, 1 => 67.6);
$userValues = array_values($z['user']);
$distanceValues = array_values($z['distance']);
$count = count($userValues);
for($i = 0; $i < $count; $i++) {
print $userValues[$i] .','. $distanceValues[$i] .'<br>';
}
- Marcus Rasmussen
-------------------------------------------------------------
On 15-04-2003 at 11:09 Ford, Mike [LSS] wrote:
-------------------------------------------------------------
> -----Original Message-----
> From: Jack Dempsey [mailto:dempsejn@georgetown.edu]
> Sent: 15 April 2003 02:48
>
> you change the names a little bit in the "what i'm looking
> for" section, but
> i'm assuming that was just shorthand:
>
> <?
>
> $z['user'] = array('vernman', 'Marc', 'rich96',
> 'lata7');
> $z['distance'] = array(0 => 0.0, 4 => 14.9, 9 => 15.0, 1 => 67.6);
>
> foreach($z['user'] as $k => $v){
> echo $v . ", " . array_shift($z['distance']) .
> "<br>";
> }
> ?>
Yeeeuch!!! Why on earth use array_shift() here?
(1) It's slow.
(2) It destroys the array.
(3) And, most importantly, it probably accesses the elements in the
wrong order!
foreach($z['user'] as $k => $v){
echo "$v, {$z['distance'][$k]}<br>";
}
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