View Full Version : Find the largest number between arrays

03-20-2003, 10:11 AM
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.

03-20-2003, 10:30 AM
$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.

03-20-2003, 10:36 AM
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 :rolleyes:

03-20-2003, 11:00 AM
if the names are supposed to go with the numbers then why don't you just use a 2 dimensional array?

03-20-2003, 11:04 AM
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 :rolleyes:
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.

03-20-2003, 12:20 PM
$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.

03-20-2003, 02:08 PM
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 :thumbsup:

03-20-2003, 05:45 PM
What Okii posted will work just fine for you. Maybe you didn't understand it?

03-20-2003, 06:03 PM
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:
it return a name instead of number...
maybe I should just change to "numbers" => "names" ? :rolleyes: