Group arrays with same index value

1

I have the following array:

Array
(
    [0] => Array
        (
            [id] => 6
            [projeto_id] => 5
            [etapa_num] => 1
            [tarefa] => teste 1 
            [status] => 0
        )

    [1] => Array
        (
            [id] => 7
            [projeto_id] => 5
            [etapa_num] => 1
            [tarefa] => teste 2
            [status] => 0
        )

    [2] => Array
        (
            [id] => 8
            [projeto_id] => 5
            [etapa_num] => 2
            [tarefa] => teste 3
            [status] => 0
        )

)

How can I group arrays that have the same index value? for example: in the code above the arrays that have the value of the step_num = 1 index, would be grouped into a "parent array".

    
asked by anonymous 18.07.2017 / 02:09

1 answer

3

Just group them by checking for a loop:

$etapas = [];
foreach($array as $key => $value) {
    $etapas[$value['etapa_num']][] = $value;
}

See working at ideone

    
18.07.2017 / 02:23