Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Find the largest number between arrays

    This is probably stupid question, but is there a buildin function to find largest number in the array?
    for example, I have array:
    $test[0] = 1
    $test[1] = 2
    $test[2] = 4
    ofcourse, I can just go through each array piece and compare the numbers, but I hope there is a simplier way to do so.
    thx.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $biggy = max($array_name);



    max() returns the numerically highest of the parameter values.

    If the first parameter is an array, max() returns the highest value in that array. If the first parameter is an integer, string or double, you need at least two parameters and max() returns the biggest of these values. You can compare an unlimited number of values.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Ökii
    [B]$biggy = max($array_name);

    thank you!
    I just found it in same minute u posted it...
    but, now I have a problem:
    First, actualy I have 'strings' with numbers I need to be compared
    Second, I have two arrays, one holds names, second holds numbers for each name.:

    $name[0] = 'James'
    $name[1] = 'John'
    $name[2] = 'Mark'

    $numbers[0] = '1'
    $numbers[1] = '14'
    $numbers[2] = '5'

    Hope this can be done in a easier way then I'm thinking of

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    if the names are supposed to go with the numbers then why don't you just use a 2 dimensional array?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Spookster
    if the names are supposed to go with the numbers then why don't you just use a 2 dimensional array?
    yes, sure, I can.
    the reason I have two seporate arrays...well...because I didnt have this idea before I wrote that part of script
    but anyway, I have to store this info in one table in the database, in this format:
    name1,number1 name2,number2 name3,number3
    then, I had to convert all this into array, then, soparate names from numbers.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $quizit = mysql_query("SELECT name, number FROM `table` ORDER BY name ASC");
    while($row = mysql_query($quizit))
    {
    $output_array[] = array($row['name'],$row['number']);
    }

    would give you

    $output_array[0][0] = 'James';
    $output_array[0][1] = '1';


    $output_array[1][0] = 'John';
    $output_array[1][1] = '14';


    $output_array[2][0] = 'Mark';
    $output_array[2][1] = '5';

    which you should be able to step through fine for echoing. You could use ORDER BY number DESC to run it backwards numerically.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #7
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, this is a good way to do, when u have two tables separate for names and numbers.
    but, I have to use only one database table, for storing both.
    and the format to store it is:
    each name folowed by coma and number, ( name1,number1 )
    and each next name separeted with space ( name1, number1 name2,number2 ) I use space to break this string into array:
    $name_number = explode(" ", $row['users']);
    well, I guess, I'd better just do comparing each number from number array. but then, how do I convert string "234" to 234 ?
    will intval work for it?

    thanks for all your replys, guys

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    What Okii posted will work just fine for you. Maybe you didn't understand it?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah, now I got it!
    thx, Okii!
    but, what do I do next?
    how can I use max(); for that array?
    it returns me: Array instead of number, and if I do double max:
    max(max($array));
    it return a name instead of number...
    maybe I should just change to "numbers" => "names" ?


  •  

    Posting Permissions

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