iColdBeZero
iColdBeZero

Reputation: 255

How to extract values from multidimensional array grouped by some value inside it using PHP?

I have an array that looks like this:-

Array ( 
 [0] => Array ( [id] => 10 [group] => 11 ) 
 [1] => Array ( [id] => 11 [group] => 13 )
 [2] => Array ( [id] => 12 [group] => 13 ) 
 [3] => Array ( [id] => 13 [group] => 13 ) 
 [4] => Array ( [id] => 14 [group] => 16 ) 
 [5] => Array ( [id] => 15 [group] => 16 ) 
 [6] => Array ( [id] => 16 [group] => 16 )  
)

For each different group in this array i want to create array that stores id's. In my example i have 3 groups, so i want to get 3 arrays like this:

Array ( [0] => 10)

Array ( [0] => 11
        [1] => 12
        [2] => 13)

Array ( [0] => 14
        [1] => 15
        [2] => 16)

Is it possible if it is how can i do it?

Upvotes: 3

Views: 186

Answers (4)

Ray Paseur
Ray Paseur

Reputation: 2194

This should help you get started. https://iconoun.com/demo/temp_icold.php

<?php // demo/temp_icold.php
/**
 * Manipulate multi-dimensional arrays
 *
 * https://stackoverflow.com/questions/45422046/how-to-extract-values-from-multidimensional-array-grouped-by-some-value-inside-i
 */
error_reporting(E_ALL);
echo '<pre>';


$original = Array (
 '0' => Array ( 'id' => 10, 'group' => 11 ),
 '1' => Array ( 'id' => 11, 'group' => 13 ),
 '2' => Array ( 'id' => 12, 'group' => 13 ),
 '3' => Array ( 'id' => 13, 'group' => 13 ),
 '4' => Array ( 'id' => 14, 'group' => 16 ),
 '5' => Array ( 'id' => 15, 'group' => 16 ),
 '6' => Array ( 'id' => 16, 'group' => 16 ),
);

print_r($original);

foreach ($original as $arr)
{
   $ndx = 'group' . $arr['group'];
   $out[$ndx][] = $arr['id'];
}

print_r($out);

Upvotes: 1

GrumpyCrouton
GrumpyCrouton

Reputation: 8621

Here is my take on this:

<?php

    $arr = array(
        array("id"=>10,"group"=>11),
        array("id"=>11,"group"=>13),
        array("id"=>12,"group"=>13),
        array("id"=>13,"group"=>13),
        array("id"=>14,"group"=>16),
        array("id"=>15,"group"=>16),
        array("id"=>16,"group"=>16)
    );

    echo "<pre>".print_r(groupsToArrays($arr),true)."</pre>";

    function groupsToArrays($arr, $groupkey = "group") {
        $main = array();
        $group = 0;
        $arr[] = array("id"=>"end", $groupkey => "0");
        foreach($arr as $key => $value) {
            if($group != $value[$groupkey]) {
                if($key != 0) $main[$group] = $tempArray;
                if($value['id'] == "end") continue;
                $group = $value[$groupkey];
                $tempArray = array();
            }
            $tempArray[] = $value;
        }
        return $main;
    }

This function will loop through your array and check the $groupkey key and will add every match to it's own array.

This is the return:

Array
(
    [11] => Array
        (
            [0] => Array
                (
                    [id] => 10
                    [group] => 11
                )

        )

    [13] => Array
        (
            [0] => Array
                (
                    [id] => 11
                    [group] => 13
                )

            [1] => Array
                (
                    [id] => 12
                    [group] => 13
                )

            [2] => Array
                (
                    [id] => 13
                    [group] => 13
                )

        )

    [16] => Array
        (
            [0] => Array
                (
                    [id] => 14
                    [group] => 16
                )

            [1] => Array
                (
                    [id] => 15
                    [group] => 16
                )

            [2] => Array
                (
                    [id] => 16
                    [group] => 16
                )

        )

)

So now you can access that group array by doing:

$groups = groupsToArrays($arr);
print_r($groups[$groupID]);

Upvotes: 1

yanman1234
yanman1234

Reputation: 1019

$result_array = array();
foreach($array_name as $sub_array){
    $result_array[$sub_array['group']][] = $sub_array['id'];
}

This will loop through your input array and create a result two dimensional array indexed by your group values. To extract a group result just index for the group as such: $result_array['GROUP_NUMBER'];

Upvotes: 1

Jim Wright
Jim Wright

Reputation: 6058

You can achieve this through many different methods, but the simplest is probably a foreach() loop. In the example below I am looping through $a (your sample array) and building up a new array called $grouped with the index as the group attributes value.

In my example I am not duplicating the ID's inside of $group. If you wanted duplicates you could remove the second if statement.

$a = [ 
    ['id' => 10, 'group' => 11],
    ['id' => 11, 'group' => 13],
    ['id' => 12, 'group' => 13],
    ['id' => 13, 'group' => 13],
    ['id' => 14, 'group' => 16],
    ['id' => 15, 'group' => 16],
    ['id' => 16, 'group' => 16],
];

$grouped = [];
foreach ($a as $entry) {
    if (! array_key_exists($entry['group'], $grouped)) {
        $grouped[$entry['group']] = [];
    }
    if (! in_array($entry['id'], $grouped[$entry['group']])) {
        $grouped[$entry['group']][] = $entry['id'];
    }
}

var_dump($grouped);

The example outputs the following:

array(3) {
  [11]=>
  array(1) {
    [0]=>
    int(10)
  }
  [13]=>
  array(3) {
    [0]=>
    int(11)
    [1]=>
    int(12)
    [2]=>
    int(13)
  }
  [16]=>
  array(3) {
    [0]=>
    int(14)
    [1]=>
    int(15)
    [2]=>
    int(16)
  }
}

Upvotes: 1

Related Questions