finding the largest of three arrays???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

finding the largest of three arrays???

Post 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????
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try using the Max() Function
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

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

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