Jump to content

php how to group a multidimensional array by a particular value?


SnarkytoSnarkers

Recommended Posts

Hopefully, I can explain this correctly...

 

I have a multidimensional array and am trying to group them according to the value of one the keys.

 

So, I'm trying to group them by level, but I won't actually know the level beforehand. So, it's not like I can put it in a for loop and say while $i < 7, because I won't know that 7 is the maximum value for the level key, and, frankly, I'm not sure that's how I would need to do it even if I did...

 

Array (
   [0] => Array (
          [cust] => XT8900
          [type] => standard
          [level] => 1
          )
   [1] => Array (
          [cust] => XT8944
          [type] => standard
          [level] => 1
          )
   [2] => Array (
          [cust] => XT8922
          [type] => premier
          [level] => 3
          )
   [3] => Array (
          [cust] => XT8816
          [type] => permier
          [level] => 3
          )
   [4] => Array (
          [cust] => XT7434
          [type] => standard
          [level] => 7
          )
)

 

What I'm hoping to produce:

 

Array (

   [1] => Array (
          [0] => Array (
                    [cust] => XT8900
                    [type] => standard
                    )
          [1] => Array (
                    [cust] => XT8944
                    [type] => standard
                    )
          )

   [3] => Array (
          [2] => Array (
                 [cust] => XT8922
                 [type] => premier
                 )

          [3] => Array (
                 [cust] => XT8816
                 [type] => permier
                 )
          )

   [7] => Array (
          [4] => Array (
                 [cust] => XT7434
                 [type] => standard
                 )
          )
)

 

 

Link to comment
Share on other sites

for lack of a fancier way, you should be able to do this with a foreach() loop:

 

$level_keys = array();
foreach ($master_array AS $k => $sub_array)
{
  $this_level = $sub_array['level'];
  $level_keys[$this_level][$k] = array('cust' => $sub_array['cust'], 'type' => $sub_array['type']);
}
'<pre>'.print_r($level_keys, TRUE).'</pre>';

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.