corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » PHP Gurus


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author PHP Gurus
Dom
Member

Registered: 13th Sep 03
User status: Offline
23rd Nov 09 at 00:28   View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
23rd Nov 09 at 00:53   View Garage View User's Profile U2U Member Reply With Quote

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
23rd Nov 09 at 01:12   View User's Profile U2U Member Reply With Quote

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
23rd Nov 09 at 08:48   View User's Profile U2U Member Reply With Quote

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
23rd Nov 09 at 09:35   View User's Profile U2U Member Reply With Quote

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
23rd Nov 09 at 15:50   View User's Profile U2U Member Reply With Quote

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
23rd Nov 09 at 16:16   View User's Profile U2U Member Reply With Quote

PAF? As in RoyalMail?
Dom
Member

Registered: 13th Sep 03
User status: Offline
23rd Nov 09 at 17:33   View User's Profile U2U Member Reply With Quote

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).

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Cooler Engine-disconnecting EGR? ffls Help Zone, Modification and ICE Advice 10 1201
18th Sep 03 at 17:35
by Juha GSi
 
Nice day so..... washed the car and took some pics! Rhino General Chat 29 1307
21st Apr 05 at 10:12
by Rhino
 
website/php/http gurus Dom Geek Day 6 306
30th Jun 05 at 16:18
by Ian
 
HELP!!!! Deekiboy Help Zone, Modification and ICE Advice 10 345
27th Jun 06 at 14:40
by Deekiboy
 
what car is this? ChazSXi General Chat 22 1138
14th Oct 07 at 04:02
by SVM 286
 

Corsa Sport » Message Board » Off Day » Geek Day » PHP Gurus 28 database queries in 0.0132601 seconds