SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict Patrick Taylor's Avatar
    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?

  2. #2
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Patrick Taylor
    $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?
    There may be a built-in function that will do this, but I don't know it.

    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)

  3. #3
    SitePoint Addict Patrick Taylor's Avatar
    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".

  4. #4
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Patrick Taylor
    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".
    Erm... did you spot the deliberate error in my code above? I was trying to itterate through the non-existant array "$myarray" rather than $my_array.

    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)

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    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); 
    Always try to let c do your heavy lifting with arrays.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I think it would be better just to do
    PHP Code:
    $maxnum max($my_array); 
    Always try to let c do your heavy lifting with arrays.
    I *knew* there would be a built-in function. Thanks for pointing it out.

    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  7. #7
    SitePoint Addict Patrick Taylor's Avatar
    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.

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Patrick Taylor
    $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.
    What you said does not make sence. Can you post the results of
    PHP Code:
    var_dump($my_array); 
    I suspect you are working with a string, not an array.

  9. #9
    SitePoint Addict Patrick Taylor's Avatar
    Join Date
    May 2003
    Location
    Manchester UK
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo $my_array
    produces this:
    534527008476254675896872564758968625 etc etc
    - it's actually made up of 3-figure numbers.
    PHP Code:
    $maxnum max($my_array);
    echo 
    $maxnum
    produces a whole set of lines like this:
    Warning: Wrong parameter count for max() in /home/username/public_html/folder/filename.php on line 145
    string(3) "124" 537
    PHP Code:
    var_dump($my_array); 
    produces this:
    124string(3) "124" 537string(3) "537" 496string(3) "496" 388string(3) "388" 219string(3) "219" 344string(3) "344" 347string(3) "347" 526string(3) "526"

  10. #10
    eschew sesquipedalians silver trophy sweatje's Avatar
    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.

  11. #11
    SitePoint Addict Patrick Taylor's Avatar
    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 
    When I echo $my_array; outside the loop (still creating it inside the loop) the printout is just one 3-figure number - the last one from the array.

    Sorry but I don't understand what you mean by:
    $my_array[] = mysql...

  12. #12
    eschew sesquipedalians silver trophy sweatje's Avatar
    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(); 
    in the loop:
    PHP Code:
    $my_array[] = $row['2']; 
    Alternativly, if you are not going to use $my_array for anything else:
    before the loop:
    PHP Code:
    $max_val 0
    in the loop:
    PHP Code:
    $max_val max($max_val$row[2]); 

  13. #13
    SitePoint Addict
    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);

  14. #14
    SitePoint Addict Patrick Taylor's Avatar
    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

  15. #15
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dtang4
    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);
    This was not his issue, it was not a long string, but rather a short string that he was replacing each time through the loop.

    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]); 
    producing:
    Code:
    array(4) {
      [0]=>
      string(3) "123"
      [1]=>
      string(3) "456"
      [2]=>
      string(3) "789"
      [3]=>
      string(2) "01"
    }
    789
    Again, try to let c do the heavy lifting, preg_match_all will be much faster than looping in PHP and calling the floor function repeatedly.

    Regards,

    Jason

  16. #16
    eschew sesquipedalians silver trophy sweatje's Avatar
    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); 
    Not sure which is faster, but both should be faster than looping in php.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •