Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Looking for help with sorting array by char length
Old 12-22-2008, 10:59 AM Looking for help with sorting array by char length
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Would anybody have any suggestions on how to sort an array elements by the strlen of the values? For example:

Turn this array:
PHP Code:
$array = array(
  
'world!',
  
'hello',
  
'hello world!'
); 
To sort like this (length descending):
Code:
Array (
  [0] => hello world!
  [1] => world!
  [2] => hello
)
I know you can do a usort to sort by an external user defined function, but I'm unsure how that works really.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-22-2008, 12:15 PM Re: Looking for help with sorting array by char length
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
PHP Code:
$array = array(
  
'world!',
  
'hello',
  
'hello world!'
);

function 
cmp($a,$b){
  return (
strlen($b)-strlen($a));
}

usort($array"cmp"); 
Not sure if it will work, but this is what I come up with after reading the usort page.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 12-22-2008, 01:35 PM Re: Looking for help with sorting array by char length
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by Insensus View Post
PHP Code:
$array = array(
  
'world!',
  
'hello',
  
'hello world!'
);
 
function 
cmp($a,$b){
  return (
strlen($b)-strlen($a));
}
 
usort($array"cmp"); 
Not sure if it will work, but this is what I come up with after reading the usort page.
Ah, very nice! I never took the proper time to fully learn how that function worked, but its starting to make sense now.

Thank you so much!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Looking for help with sorting array by char length
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18279 seconds with 12 queries