There is an array of this type:

Array ( [0] => Array ( [numberRow] => 1 [columnId] => 1 [columnValue] => 1 ) [1] => Array ( [numberRow] => 1 [columnId] => 2 [columnValue] => 10 ) [2] => Array ( [numberRow] => 1 [columnId] => 3 [columnValue] => 245tgvae ) [3] => Array ( [numberRow] => 2 [columnId] => 1 [columnValue] => 2 ) [4] => Array ( [numberRow] => 2 [columnId] => 2 [columnValue] => 20 ) [5] => Array ( [numberRow] => 2 [columnId] => 3 [columnValue] => sg45t ) ) 

I need to group it by the numberRow key to make it look like this:

 Array ( [0] => Array ( [0] => Array ( [numberRow] => 1 [columnId] => 1 [columnValue] => 1 ) [1] => Array ( [numberRow] => 1 [columnId] => 2 [columnValue] => 10 ) [3] => Array ( [numberRow] => 1 [columnId] => 3 [columnValue] => 245tgvae ) ) [1] => Array ( [0] => Array ( [numberRow] => 2 [columnId] => 1 [columnValue] => 2 ) [1] => Array ( [numberRow] => 2 [columnId] => 2 [columnValue] => 20 ) [2] => Array ( [numberRow] => 2 [columnId] => 3 [columnValue] => sg45t ) ) ) 

    1 answer 1

     function _group_by($array, $key) { $resultArr = []; foreach($array as $val) { $resultArr[$val[$key]][] = $val; } return $resultArr; } 

    input data:

     $test = [ [ 'numberRow' => 1, 'columnId' => 1, 'columnValue' => 1 ], [ 'numberRow' => 1, 'columnId' => 2, 'columnValue' => 10 ], [ 'numberRow' => 1, 'columnId' => 3, 'columnValue' => '245tgvae' ], [ 'numberRow' => 2, 'columnId' => 1, 'columnValue' => 2 ], [ 'numberRow' => 2, 'columnId' => 2, 'columnValue' => 20 ], [ 'numberRow' => 2, 'columnId' => 3, 'columnValue' => 'sg45t' ] ]; 

    Using:

     print_r(_group_by($test, 'numberRow')); 

    Feel here: https://ideone.com/xd0pEt

    Borrowed from https://stackoverflow.com/a/15102185/6104996

    • Everything is correct but do not play [],[],[] and variable operators of the same name. for example, you unintentionally delete $ and get no error, and then you will look for the cause of the bug .... - Naumov
    • @Naumov did not understand what you mean? Leave your subjective opinion about [],[],[] with you please - Alex Shimansky
    • @ Alexey Shimansky, thank you very much! - gudfar
    • 3
      @Naumov Once again, keep your subjective opinion with you. If your religion does not allow you to use the fact that php introduced in version 5.4 then this is purely your moral and spiritual problems. - Alexey Shimansky
    • five
      @Naumov yes. now your and zend framework .... uh ... version 1.12 with the presence of version 2.4 ...... You probably understand something too literally. They do not dictate the world standard. These are recommendations on how to design a code in a SPECIFIC framework. In THEIR framework. Other frameworks have their own design rules. You can investigate this question and be convinced of it. They are not a strict requirement to follow this rule everywhere ........ Moreover, one can argue very much with their rules. Only one substitution of variables in double quotes is worth something .... In the meantime, this is all YOUR PERSONAL nerves - Alexey Shimansky