TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-15-2008, 01:33 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default 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?
benton is offline  
Reply With Quote
Old 07-15-2008, 03:51 AM   #2 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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.
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 07-15-2008, 11:42 AM   #3 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

From where do you get your array? Database or somewhere else?
Can you provide more info?
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 07-15-2008, 12:10 PM   #4 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by drewbee View Post
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 is offline  
Reply With Quote
Old 07-15-2008, 12:15 PM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by abiko View Post
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.
benton is offline  
Reply With Quote
Old 07-15-2008, 12:32 PM   #6 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

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
        )

)
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 07-15-2008, 01:16 PM   #7 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

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?
benton is offline  
Reply With Quote
Old 07-15-2008, 02:50 PM   #8 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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.
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 07-15-2008, 04:24 PM   #9 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

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.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


All times are GMT. The time now is 12:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design