Page 1 of 1

finding the largest of three arrays???

Posted: Mon Feb 27, 2006 12:16 pm
by raghavan20
This is a simple problem and I am thinking of using either PHP functions or by some logic to do it with minimum number of steps....

I have got three arrays which has unknown number of elements in each. We have to find which array has the maximum number of elements in it...


few ways i have thought of:
1. using a simple if:
a>b & a>c
then a
else b > c
then b else c

2. this can be done in a line using ternary operators

3. assigning arrays and their count as associative arrays and sorting by keys which can help us get the array with maximum elements
array('a'=>count(a), 'b' => count(b), 'c' => count(c))
sort by key...and last element would be having max number of elements.


can you guys think of anything better????

Posted: Mon Feb 27, 2006 12:18 pm
by hawleyjr
Try using the Max() Function

Posted: Mon Feb 27, 2006 12:35 pm
by John Cartwright

Code: Select all

$arrayCount['array1'] = count($array1);
$arrayCount['array2'] = count($array2);
$arrayCount['array3'] = count($array3);

$max = max($arrayCount);
Something like that?