SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
Thread: Find highest number in array
-
Aug 25, 2004, 02:02 #1
- Join Date
- May 2003
- Location
- Manchester UK
- Posts
- 219
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Find highest number in array
$my_array is an array of 3-figure numbers which is from a MySQL query using GROUP BY.
echo $my_array; prints all the 3-figure numbers joined together, eg: 456184562784825675983726 etc etc (bolded just for clarification).
How can I find the highest 3-figure number with PHP?
-
Aug 25, 2004, 02:23 #2
- Join Date
- Nov 2003
- Location
- England
- Posts
- 293
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Patrick Taylor
Try
PHP Code:$maxnum = 0;
foreach($myarray as $num){
if($num > $maxnum) $maxnum = $num;
}
echo "The highest value in the array is $maxnum";
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
-
Aug 25, 2004, 02:41 #3
- Join Date
- May 2003
- Location
- Manchester UK
- Posts
- 219
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks. I tried it and got this (lots of rows of it - one row for each of the different values in the array):
"Warning: Invalid argument supplied for foreach()" etc, then at the end of all the warnings: "The highest value in the array is 0".
-
Aug 25, 2004, 02:58 #4
- Join Date
- Nov 2003
- Location
- England
- Posts
- 293
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Patrick Taylor
Oops. Sorry about that.
Should have been...
PHP Code:$maxnum = 0;
foreach($my_array as $num){
if($num > $maxnum) $maxnum = $num;
}
echo "The highest value in the array is $maxnum"
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
-
Aug 25, 2004, 03:08 #5
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think it would be better just to do
PHP Code:$maxnum = max($my_array);
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Aug 25, 2004, 03:28 #6
- Join Date
- Nov 2003
- Location
- England
- Posts
- 293
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by sweatje
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
-
Aug 25, 2004, 04:00 #7
- Join Date
- May 2003
- Location
- Manchester UK
- Posts
- 219
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, I spotted the deliberate mistake before I tried your code. $maxnum = max($my_array); doesn't work, because apparently php requires the actual array values separated by commas and spaces, and not just the array variable. So I was thinking of some array function or something that sends the highest value to the end of the array, then trying to pick out that value, so far without success.
-
Aug 25, 2004, 04:12 #8
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Patrick Taylor
PHP Code:var_dump($my_array);
-
Aug 25, 2004, 05:21 #9
- Join Date
- May 2003
- Location
- Manchester UK
- Posts
- 219
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:echo $my_array;
534527008476254675896872564758968625 etc etc
PHP Code:$maxnum = max($my_array);
echo $maxnum;
Warning: Wrong parameter count for max() in /home/username/public_html/folder/filename.php on line 145
string(3) "124" 537PHP Code:var_dump($my_array);
124string(3) "124" 537string(3) "537" 496string(3) "496" 388string(3) "388" 219string(3) "219" 344string(3) "344" 347string(3) "347" 526string(3) "526"
-
Aug 25, 2004, 05:42 #10
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It looks to me like you are running var_dump($my_array) in a loop where $my_array is a three character string every time you look at it.
I assume you have $my_array = mysqlblahblah() somewhere. Try doing $my_array[] = mysql... instead.
-
Aug 25, 2004, 05:54 #11
- Join Date
- May 2003
- Location
- Manchester UK
- Posts
- 219
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
$my_array comes from a query using GROUP BY, which gives a set of results which include a number on each row ($row['2']). It's that number I want the highest one of.
PHP Code:while ($row = mysql_fetch_assoc($result)) {
$my_array = $row['2'];
etc
Sorry but I don't understand what you mean by:
$my_array[] = mysql...
-
Aug 25, 2004, 06:57 #12
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
before the loop:
PHP Code:$my_array = array();
PHP Code:$my_array[] = $row['2'];
before the loop:
PHP Code:$max_val = 0;
PHP Code:$max_val = max($max_val, $row[2]);
-
Aug 25, 2004, 07:09 #13
- Join Date
- Jan 2002
- Location
- NJ/NY
- Posts
- 346
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You need to first split the string into an array of 3-digit numbers. I don't know if there's a function to do that, but you can try something like this (code below has not been tested)...
HTML Code:while($my_array > 0) { $array[] = ($my_array/1000 - floor($my_array/1000)) * 1000; $my_array = floor($my_array/1000); } function_call($array);
Forget WordPress. Launch a 300,000+ page autoscaling Job Search.
FREE GUIDES
Google Rapid Indexer - 21K in 21 Days - 7 Steps to Start a Site
-
Aug 25, 2004, 07:24 #14
- Join Date
- May 2003
- Location
- Manchester UK
- Posts
- 219
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
dtang4 - thanks for the suggestion. I have it working after sweatje's last post.
sweatje, your first option produced "wrong parameter count for max()". I was under the impression a single value in the brackets wouldn't work with "max" - don't know - but your second option did the job.
A big "thankyou" from me. Nice work.
Regards,
Patrick
-
Aug 25, 2004, 07:35 #15
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by dtang4
However, if it were a long string, I would have choosen an approach like this rather than your code:
PHP Code:$test = '12345678901';
preg_match_all('/(\d{1,3})/', $test, $matches);
var_dump($matches[1]);
echo max($matches[1]);
Code:array(4) { [0]=> string(3) "123" [1]=> string(3) "456" [2]=> string(3) "789" [3]=> string(2) "01" } 789
Regards,
Jason
-
Aug 25, 2004, 07:43 #16
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
alternativly, if you do not care for regular expresions:
PHP Code:$my_array = explode("\n",chunk_split($test,3));
var_dump($my_array);
echo max($my_array);
Bookmarks