0

I need to sort a dataset in php, It has this kind of structure:

$arr['col1'][0] = '12a';
$arr['col2'][0] = '6';
$arr['col1'][1] = '05m';
$arr['col2'][1] = '7l';

and I want to do something like this SQL:

select *
from $arr
order by col1, col2

I tryed this, but does not work

$col_1 = array_column($arr, 'col1');
$col_2 = array_column($arr, 'col2');
array_multisort($col_2, $col_1, $arr);

it gives "array sizes inconsistent"

desired order is:

05m  7l
12a  6
DDS
  • 1,821
  • 10
  • 26
  • 4
    For that `array_column` to work, the data structure should be `[['col1' => '12a', ...], ...]`, not `col1` on the outer array. It's rather unclear what's supposed to relate to what here. Have you tried https://stackoverflow.com/a/17364128/476? – deceze Aug 02 '19 at 14:21
  • Sorry but I don't get the difference between the two notations, it looks it's the very same thing: array containing arrays – DDS Aug 02 '19 at 14:37
  • For contrast, you currently have `['col1' => ['12a', ...], ...]`. – deceze Aug 02 '19 at 14:38
  • is there a way I can convert my structure without having to rewrite it? – DDS Aug 02 '19 at 14:39
  • Surely it's possible… But's it's unclear whether that's necessary at all. I don't know what data structure you *need*. Maybe your current one is already exactly what you need and you just need to adjust the sorting algorithm. Or maybe you really should be changing your data structure to suit your problem better. It's unclear enough what you *want* to do here that I can't say one way or the other. – deceze Aug 02 '19 at 14:43

0 Answers0