Php For each Limit

Hi!
I have a problem.
My code generates data from a while loop. I get the distance, and an ID.

Now I need those two data to be linked together (the distance is good for only one id) I need to sort the data by distance and display only the first 5.

I first tried to put the data in an array like this :
$array[ID] = distance

And then sort it…

But I cannot get only the first 5 (shortest distance) by using a for each.

Anyone got a better idea??

thanks a lot!

Why not use a regular for instead of a foreach?

http://us2.php.net/manual/en/control-structures.for.php

for ($count = 0; $count < 5; $count++)
{
    $array[$ID] = $distance;
}

Because the problem is not getting the data in an array, it’s getting it out…

foreach ($array as $value) {
	echo $value;
}

But only get the first five value

Okay, so then you want something like…

var $count = 1;

foreach ($array as $value)
{
  echo $value;

  if ($count > 5)
  {
    break;
  }

  $count++;
}

Exactly
Thanks a lot!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service