There is for example my type array:

array = [ 0 = [ Propery = '1'; ] 1 = [ Propery = '2'; ] 2 = [ Propery = '1'; ] 3 = [ Propery = '3'; ] 4 = [ Propery = '2'; ] 5 = [ Propery = '1'; ] ] 

At the output I want to get an array of type

 array = [ 0 = [ 0 = [ Propery = '1'; ] 2 = [ Propery = '1'; ] 5 = [ Propery = '1'; ] ] 1 = [ 1 = [ Propery = '2'; ] 4 = [ Propery = '2'; ] ] 3 = [ 3 = [ Propery = '3'; ] ] ] 

That is, to get all the arrays, sorted by groups for some indicator. I tried to write myself, but it was too cumbersome and bad code. Maybe there is some kind of universal solution using std. php functions?

  • Can I have reproducible arrays? In the format of the source code php, but not var_dump (Or what is this for your format?) - vp_arth
  • @vp_arth this I for example, introduced that there are types of arrays in which the parameter, and I need to group them by it - Oleksandr

2 answers 2

It is not entirely clear what the complexity of this transformation is. If, in fact, the transformation is more complicated than the example given, it is better to supplement the question.

For initial data of the form:

 $data = [ [ 'property' => 1 ], [ 'property' => 2 ], [ 'property' => 1 ], [ 'property' => 3 ], [ 'property' => 2 ], [ 'property' => 1 ], ]; 

The desired result will be obtained using the following cycle:

 $result = []; foreach($data as $k => $v){ $result[$v['property']][$k] = $v; } 
     foreach($array as $k => $val): switch (creteria_definition): case 'creteria_1': $new_array[0][$k] = $v; dreak; case 'creteria_2': $new_array[1][$k] = $v; dreak; case 'creteria_3': $new_array[2][$k] = $v; dreak; ebdswitch; endforeach; 
    • Unfortunately, I have an option here - an example. So I can have them of 30 different values ​​of the parameters. And each write through the switch is not very logical. - Oleksandr
    • It is unlikely that you will find a more simplified function, where you can compare the 30 criteria with the value of each element of the array and sort it. But if you find it, let me know, I already thought about it myself. - Kirill Korushkin