Dom
Member
Registered: 13th Sep 03
User status: Offline
|
Hi all,
Does anyone know if there is a better/another way of getting the top 5 out of an array (thinking FOR statement perhaps? Or can you add further conditions/expressions to the while loop, similar to an IF statement ie: cond && cond etc)?
code: $arr = array("a" => "10", "b" => "6", "c" => "8", "d" => "16", "e" => "3", "f" => "11.5", "g" => "22.1");
asort($arr);
$i = 1;
while (list($key, $value) = each($arr)) {
if ($i > 5) break;
echo "Key: $key; Value: $value<br />\n"; //Dump Top 5
$i++;
}
The indexes/keys aren't known in the array (will be 100+ keys), so i can't just call $arr["bob"] etc And i'm trying to stay away from creating 2D Arrays (no point creating 100+ 2Ds, using array_chunk() etc, if i only need the first 5 results).
Chars
EDIT -
Can get rid of the IF statement and change position of the increment by changing the while statement to
code: while ((list($key, $value) = each($arr)) && $i < 5 && $i++) {
probably about as small as i'm going to make this, but is this an efficient way of doing it? Quicker/Better methods?
[Edited on 23-11-2009 by Dom]
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
I'm not 100% clear on the problem but I would be having a database table.
|
xa0s
Banned
Registered: 4th Mar 08
Location: Dartford, Kent Car: Turbo'd Fabia vRS
User status: Offline
|
quote: Originally posted by Ian
I'm not 100% clear on the problem but I would be having a database table.
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
quote: Originally posted by xa0s
quote: Originally posted by Ian
I'm not 100% clear on the problem but I would be having a database table.
Me too. Pulling out of the database will be much easier too as you can limit the number of records returned so could easily pick out the first 5, 10, 20 or whatever.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
You are using an array with a key index instead of a numeric one so its always going to be ineffient to try and refer to the objects in the array as numbers (i.e. top 5)..
If you use an integer indexed array you can use a while loop to go through the top 5 e.g
while (($i < 5) && ($i < count($array))
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
No worries, was talking cack as i thought there was an issue with this part of my PAF Geosearch function but it's not. Just redesigned how the function returned the results so i've managed to get around this all. Cheers anyways.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
PAF? As in RoyalMail?
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
quote: Originally posted by DaveyLC
PAF? As in RoyalMail?
yeah, although it's silly money (although there is a leak if you hunt) if you're just after long/lats as they aren't that accurate. Better off getting the £120 db from Linuxbox.co.uk as the longs/lats are hugely accurate to 13 decimal places (royalmails is 6).
|