Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / PHP Arrays

Author
Message
AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 24th Sep 2006 21:22 Edited at: 24th Sep 2006 21:24
Hi,
I have got an array called $scorearray which goes from 0 to 4


how can i find out which $scorearray is the biggest?

Thanks,
Alex


Gamedesign er20
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: The one place you would never look...
Posted: 24th Sep 2006 21:32
What type of question is that?

Cocacola and Pepsi aren't that differnt. Deal with it.
Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 24th Sep 2006 21:45
You could sort the array then pick the last element

AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 24th Sep 2006 21:47
How do i sort the array? I dont know much php


Mikey P
19
Years of Service
User Offline
Joined: 23rd May 2005
Location: Manchester, UK
Posted: 24th Sep 2006 21:50
Do you mean like which has the biggest value, or like how many elements you have in it (like 4 in your example)?

If you want to find out how many elements, just do:



AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 24th Sep 2006 22:01 Edited at: 24th Sep 2006 22:02
i need to find out which element in the array is the biggest value


AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 24th Sep 2006 22:18
I worked it out as kentaree said about sorting.

sort($scorearray);

where $scorarray[4]; is the largest

Thanks,
Alex


Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 25th Sep 2006 01:08
Just an idea - the PHP Documentation is fantastic (one of the best on the web imho) and your answer could have been found there within about 10 seconds. This isn't me having a go at you and telling you to search first, but it would have saved you about 40 minutes of your life!

There are some fantastic things you can do with arrays. Take a look through the commands.

[center]
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 25th Sep 2006 05:58
Or failing that, working out which number is the largest isn't really that hard anyway. Store the first element as the current largest, loop through the rest, checking if each one beats the 'current largest', if it does, replace it. By the end, you'll have the largest number in the array.

Obviously using the in-built functions is easier, but my point is that if you just thought about it a while, you could've written your own function anyway.


"Computers are useless, they can only give you answers."
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 25th Sep 2006 14:30
The MAX command can be used to find highest value in an array, you can then use the ARRAY_SEARCH command to tell which key that value belongs to, although if you have multiple keys with same value then you may a problem, but if all you want is the highest value then MAX works just great.



Boo!
Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 25th Sep 2006 14:41
What happens if you have multiple identical high scores...

[center]
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 26th Sep 2006 01:17
Then array-search will only return the first value.


Formally known as RTSpider

Login to post a reply

Server time is: 2024-11-17 15:39:35
Your offset time is: 2024-11-17 15:39:35