How can I extract values ​​from a multidimensional array grouped by some value within 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 group in this array, I want to create an array that stores the IDs. In my example, I have 3 groups, so I want to get 3 arrays, for example:

Array ( [0] => 10)

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

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

      

Is this possible, how can I do this?

+3


source to share


4 answers


This will 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);
      

Run codeHide result


+1


source


You can achieve this with different methods, but the simplest is probably the foreach () loop . In the example below, I go through $a

(your sample array) and create a new array named $grouped

with the index as the attribute value group

.

In my example, I am not duplicating the ID inside $group

. If you need duplicates, you can remove the second statement if

.

$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)
  }
}

      

+1


source


$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 2D results array indexed with your group values. To extract a grouped result, just an index on the group itself:$result_array['GROUP_NUMBER'];

+1


source


Here's my example:

<?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 key $groupkey

and add each match to its 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 this array of groups by doing the following:

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

      

+1


source







All Articles