TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How to sort a multi-array (http://www.talkphp.com/general/3122-how-sort-multi-array.html)

benton 07-15-2008 01:33 AM

How to sort a multi-array
 
I have an array like this
PHP Code:

 Array
(
    [
1] => Array
        (
            [
52] => Milk Chocolate
            
[221] => Vanilla Bean
            
[212] => Banana Cream
            
[80] => Chocolate Peanut Butter
        
)



I need to have it sorted on the value. The only examples I can find for doing this are those using multisort but that resets the keys, which won't work in this case. I tried using usort with a callback function like this but it failed.
PHP Code:

function cmp($a$b)
{
    if (
$a == $b) {
        return 
0;
    }
    return (
$a $b) ? -1;
}
foreach (
$myarray as $sub)
 
$myarray[$sub] = usort($subcmp); 

Would someone please point out my mistake or how best to do this?

drewbee 07-15-2008 03:51 AM

I believe asort is what your looking for.
PHP Code:

$array = array('1' => array('52' => 'Milk Choclate',
                            
'221' => 'Vanilla Bean',
                            
'212' => 'Banana Cream',
                            
'80' => 'Choclate Peanut Butter'));

asort($array['1']);
echo 
print_r($array); 


I am not to sure if this is exactly what you are looking for. If you could provide a more accurate example as if I were to build a recursive function for this (multi-dim array);

It would need to start at the deepest array and work back to the beginning (what string value does an array have?) if you see what I mean.

abiko 07-15-2008 11:42 AM

From where do you get your array? Database or somewhere else?
Can you provide more info?

benton 07-15-2008 12:10 PM

Quote:

Originally Posted by drewbee (Post 17067)
I believe asort is what your looking for.
PHP Code:

$array = array('1' => array('52' => 'Milk Choclate',
                            
'221' => 'Vanilla Bean',
                            
'212' => 'Banana Cream',
                            
'80' => 'Choclate Peanut Butter'));

asort($array['1']);
echo 
print_r($array); 


I am not to sure if this is exactly what you are looking for. If you could provide a more accurate example as if I were to build a recursive function for this (multi-dim array);

It would need to start at the deepest array and work back to the beginning (what string value does an array have?) if you see what I mean.

I don't understand what you mean by "what string value..." but I added the following based on what you said and it works great. But it doesn't do it backwards so I'm wondering if it OK to use?
PHP Code:

         for ($i 0$i count($myarray); ++$i)
         {
           if (
is_array($myarray[$i]))
             
asort($myarray[$i]);
         } 


benton 07-15-2008 12:15 PM

Quote:

Originally Posted by abiko (Post 17073)
From where do you get your array? Database or somewhere else?
Can you provide more info?

The array is generated dynamically from data read from a database but controlled by items not part of the database. So the array can end up like
PHP Code:

 Array
(
    [
1] => Array
        (
            [
212] => Banana Cream
            
[80] => Chocolate Peanut Butter
            
[52] => Milk Chocolate
            
[221] => Vanilla Bean
        
)

    [
3] => Array
        (
            [
45] => Alpine Punch
            
[194] => Apricot
        
)



So there may be ten items read in from the database but, in the above example, only items 1 and 3 will be added to the array. Then the key and value of each array element is loaded from the database data for that item.

abiko 07-15-2008 12:32 PM

Is this a solution to your problems?
PHP Code:

<?php

$array 
= array(   '1' =>  array(  '21'  =>  'Item 1',
                                  
'928' =>  'Item what!?!',
                                  
'86'  =>  'Too late item',
                                  
'153' =>  'What is happening here?!?'

                                
),
                  
'2' =>  array(  '997' =>  'Fruit - apple'
                                  
'77' => 'Another fruit - orange',
                                  
'4'   =>  'The icky fruit',
                                  
'64'  =>  'Watermelon'
                                
)
                );
                
/*
 *  Sort the items
 */
 
 
foreach ( $array as $row => $data) {
      
ksort$data);
      
$_compile[$row] =  $data;
 
 } 
 
 
/*
  * output
  */
  
  
echo "<h1>First array</h1>";
  
print_r $array);
  
  echo 
"<h1>The compiled array</h1>";
  
print_r$_compile );                  
      

?>

Output
Code:

Array
(
    [1] => Array
        (
            [21] => Item 1
            [86] => Too late item
            [153] => What is happening here?!?
            [928] => Item what!?!
        )

    [2] => Array
        (
            [4] => The icky fruit
            [64] => Watermelon
            [77] => Another fruit - orange
            [997] => Fruit - apple
        )

)


benton 07-15-2008 01:16 PM

No, that doesn't work. What does $_compile[$row] represent? I tried inserting the array name there but it didn't work either. The code I posted does seem to work. Is there anything wrong with using a for loop?

drewbee 07-15-2008 02:50 PM

Hi Benton,

So what I understand is that you will have a key index housing a sub-array.

So what you gave will work perfectly fine.

PHP Code:

         for ($i 0$i count($myarray); ++$i
         { 
           if (
is_array($myarray[$i])) 
             
asort($myarray[$i]); 
         } 


You do not need to go backwards with this; I thought you actually needed to traverse (crawl down) the array.

benton 07-15-2008 04:24 PM

Thanks for letting me know. I've tried a bunch of tests and it seems to work fine. Thanks for providing the code to make it work.


All times are GMT. The time now is 09:01 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0