Ok...maybe I am having a dumb moment here but I am confused as to how to use this code lol
My array that I am wanting to sort is called $phrases...can you show me how to
'plug it into" the above code?
thanks
Main Topics
Browse All TopicsI need to sort an array by string size....the array has around 8500 elements and each element has multiple strings, seperated by the pipe ( | ) character. Example:
element[0] = word1|word2|word3
element[1] = multiple words set 1|multiple words set 2| multiple words set 3
element[2] = multiple words set 1|multiple words set 2
element[3] = long sentence of words set 1|long sentence set 2
What I need to do is sort the array by the elements with the MOST AMOUNT OF WORDS in each element, largest to smalles.
So inthe above example the sorted array will look like this:
element[0] = long sentence of words set 1|long sentence set 2 (has 6 words max)
element[1] = multiple words set 1|multiple words set 2| multiple words set 3 (has 4 words max)
element[2] = multiple words set 1|multiple words set 2 (has 4 words max)
element[3] = word1|word2|word3 (has 1 word max)
Hopefully that makes sense.....
Please provide the code and not a link to a resource. thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yup I tried that thinking that you messed that part up :) I just did it again to make sure that I did it correctly and when I change the foreach loop to the proper syntax I get this error:
Warning: Wrong parameter count for explode() for this line:
$phrases = explode($string);
and this error:
Warning: Invalid argument supplied for foreach() for this line:
foreach ($phrases as $phrase)
sorry, the explode function will turn a string into an array based on a delimiter
the first explode should be
explode("|", $string) // separates the phrases into it's various phrases delimited by the pipe | character
the second explode shoudl be
explode(" ", $phrase) // separates the phrases into words delimited by a space
Got it?
Business Accounts
Answer for Membership
by: mikeadaPosted on 2009-11-03 at 12:15:52ID: 25733062
usort($element, "cmp");
function cmp($ele1, $ele2)
{
$max1 = maxwords($ele1);
$max2 = maxwords($ele2);
if ($max1 == $max2)
return 0;
return ($max1 > $max2) ? -1 : 1;
}
function maxwords($string)
{
$maxwords = 0;
$phrases = explode($string);
foreach ($phrase in $phrases)
{
// split the phrase up into words and count them
$wordcount = count(explode($phrase));
if ($wordcount > $maxwords)
$maxwords = $wordcount;
}
return $maxwords;
}