Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group the array by the same key and value

Tags:

arrays

php

I have an array like this:D

Array
(
[0] => Array
    (
        [type] => AAA
        [label_id] => A1,35
    )

[1] => Array
    (
        [type] => AAA
        [label_id] => A2,34
    )

[2] => Array
    (
        [type] => BBB
        [label_id] => B1,29
    )

[3] => Array
    (
        [type] => CCC
        [label_id] => C1,20
    )

[4] => Array
    (
        [type] => CCC
        [label_id] => C2,19
    )

[5] => Array
    (
        [type] => CCC
        [label_id] => C3,18
    )
)

Now i would like to make it group by the same key and value like this.

Array
(
[0] => Array
    (
        [type] => AAA
        [label_id] => Array
            (
                [0] => A1,35
                [1] => A2,34
            )
    )
[1] => Array
    (
        [type] => BBB
        [label_id] => Array
            (
                [0] => B1,29
            )
    )
[2] => Array
    (
        [type] => CCC
        [label_id] => Array
            (
                [0] => C1,20
                [1] => C2,19
                [2] => C3,18
            )
   )
   )

does any one know how to do that?

like image 300
408 Avatar asked Aug 24 '12 11:08

408


People also ask

How do you group an array of objects using a key?

The most efficient method to group by a key on an array of objects in js is to use the reduce function. The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

How do you group an array?

The group() method groups the elements of the calling array according to the string values returned by a provided testing function. The returned object has separate properties for each group, containing arrays with the elements in the group. This method should be used when group names can be represented by strings.

Can array have key value pairs?

Arrays in javascript are typically used only with numeric, auto incremented keys, but javascript objects can hold named key value pairs, functions and even other objects as well. Simple Array eg. We see above that we can loop a numerical array using the jQuery.


2 Answers

This should do the trick

$args = array
(
    array( 'type' => 'AAA', 'label_id' => 'A1,35' ),
    array( 'type' => 'AAA', 'label_id' => 'A2,34' ),
    array( 'type' => 'BBB', 'label_id' => 'B1,29' ),
    array( 'type' => 'CCC', 'label_id' => 'C1,20' ),
    array( 'type' => 'CCC', 'label_id' => 'C2,19' ),
    array( 'type' => 'CCC', 'label_id' => 'C3,18' )  
);

$tmp = array();

foreach($args as $arg)
{
    $tmp[$arg['type']][] = $arg['label_id'];
}

$output = array();

foreach($tmp as $type => $labels)
{
    $output[] = array(
        'type' => $type,
        'label_id' => $labels
    );
}

var_dump($output);

The output is :

array
  0 => 
    array
      'type' => string 'AAA' (length=3)
      'label_id' => 
        array
          0 => string 'A1,35' (length=5)
          1 => string 'A2,34' (length=5)
  1 => 
    array
      'type' => string 'BBB' (length=3)
      'label_id' => 
        array
          0 => string 'B1,29' (length=5)
  2 => 
    array
      'type' => string 'CCC' (length=3)
      'label_id' => 
        array
          0 => string 'C1,20' (length=5)
          1 => string 'C2,19' (length=5)
          2 => string 'C3,18' (length=5)
like image 181
Anthony Simmon Avatar answered Sep 20 '22 17:09

Anthony Simmon


<?php

$grouped_types = array();

foreach($types as $type){
        $grouped_types[$type['type']][] = $type;
}

?>
like image 37
Wayne Whitty Avatar answered Sep 21 '22 17:09

Wayne Whitty