Jump to content

sorting a multidimensional array


mainstreetop

Recommended Posts

Someone from this forum recently helped me with structuring an array.  It outputs as...

 

Array ( [525] => Array ( [binders] => 992 )

          [372] => Array ( [Corporate Kits] => 105 )

          [521] => Array ( [binder Pockets] => 10 )

          [431] => Array ( [Desktop Supplies Organizers] => 2 )

          [522] => Array ( [binder Posts] => 3 ) )

 

          [brand_id] => Array ( [category] => item_count )

 

How would I sort the array by category and preserve the keys?  I've tried array_multisort(), but can't seem to figure out how to refer to the ['category'] key.

 

Thanks

Mike

Link to comment
Share on other sites

KHR2003 -

 

Thank you for responding.  I am trying to sort the array all the way down.

 

I wound up rearranging the array as below and then using ksort().  However, I'm sure this is not the most efficient way to accomplish the task.

foreach($levels as $category => $value) {
    foreach($value as $id => $count)  {
        $new[$level] = array($key => $count);
    }
}
ksort($new);

 

Link to comment
Share on other sites

Barand -

 

You're right... this does seem overly-complicated.  I guess this all started because I need three pieces of data... id, category and count.  I need the category id because it is the only unique field.  Category can appear more than once in the DB.

 

I used the array approach to group the id's and get a count.  It seemed running another mysql query was too taxing.  Plus, being new to PHP, I felt this would help understand arrays a little better.

 

I imagine this does not paint a full picture without seeing the code, but I wanted to give some insight as to why I traveled down this path.

Mike

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.