3

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
iColdBeZero
  • 255
  • 2
  • 11

4 Answers4

1

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);
Ray Paseur
  • 2,106
  • 2
  • 13
  • 18
  • Thank you very much for your answer, this will do for the task i had, will you mind to provide me some documentations to read on this topic? i read php documentation, everything i could find on stackoverflow here, but still have very slight idea of how this works. Thank you again! – iColdBeZero Jul 31 '17 at 17:49
  • @iColdBeZero I provided a brief explanation of this methodology on my answer, but the basic idea is to provide indexes to your result array that act as groups. Then add to the corresponding index that represents your group. Look more into indexing arrays and the `foreach` loop for more info though. – yanman1234 Jul 31 '17 at 17:53
  • It takes some time to learn PHP (or any other language). In my experience the resources at SitePoint are really good. – Ray Paseur Jul 31 '17 at 18:01
  • @iColdBeZero this is not your expected outcome what this code is giving:-https://eval.in/839853 . So what make you to mark this as an accepted answer? – Alive to die - Anant Jul 31 '17 at 18:05
  • @AlivetoDie This is exactly what they wanted. Are you maybe being confused by the input array being printed first? – yanman1234 Jul 31 '17 at 18:09
  • @yanman1234 no i am not confused. check question desired output and my answer link output :- https://eval.in/839850. And now compare it with current selected answer output:-https://eval.in/839853 – Alive to die - Anant Jul 31 '17 at 18:11
  • 1
    @Alive to Die: I don't get why you think there is something wrong with the answer. It produces the three arrays that the question asked for. There are lots of ways to get the answer and organize the data. What are we missing here? – Ray Paseur Jul 31 '17 at 18:12
  • i din't say it's wrong. It's not exactly what shown by OP that's it. Leave it man. Op moved out, so we have to moved out too – Alive to die - Anant Jul 31 '17 at 18:15
  • @AlivetoDie The only difference between your result and this result is the names used for grouping in the mutidim array, which was not specified by the OP. So this is very much correct. – yanman1234 Jul 31 '17 at 18:16
1

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)
  }
}
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
1
$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'];

yanman1234
  • 1,009
  • 9
  • 27
1

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]);
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71