Jump to content

Sorting Arrays


moon 111

Recommended Posts

No. That won't help me. I already tried that.

 

Example:

 

$title = array('Hello!', 'Title #2', 'A title!');
$description = array('message', 'blah', 'text');

 

If I sort $title and $description seperately it will only mess it up. I want to sort $title and then apply the same thing to $description without mixing up the values.

 

What I want the arrays to look like after the sort is:

 

$title = array('A title!', 'Title #2', 'Hello!');
$description = array('text', 'blah', 'message');

 

Get it?

Link to comment
Share on other sites

I don't understand what type of sort your performing from your example, it is neither alphabetical or numerical. If your trying to do a custom sort you need usort().

Link to comment
Share on other sites

You can stick them in a multidimensional array and use a user defined sort function.

 

<?php
$title = array('Hello!', 'Title #2', 'A title!');
$description = array('message', 'blah', 'text');
$array= array();
for($x=0;$x<count($title);$x++){
   $array[$x]['title'] = $title[$x];
   $array[$x]['description'] = $description[$x];
}
function mysort($a,$b){
   return strcmp($a['title'],$b['title']);
}
echo '<pre>'.print_r($array,1).'</pre>';
usort($array,'mysort');
echo '<pre>'.print_r($array,1).'</pre>';
?>

 

The returned array might not quite be what you were expecting, but it may actually be more useful.

 

See: http://www.php.net/usort

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.