I have this array structure
Array
(
[xyz] => Array
(
[1] => 3
[0] => s
)
[dds] => Array
(
[a] => 96
[d] => 4
)
...
)
How do I sort on the first dimension so that dds would be first. ksort does not work (at least for me).
TIA
edit
Here is my simple code. I read a serialized 2D array file into a variable, and do a sort on it, nothing complicated, ksort works on my 1D array without a problem. If I comment out the ksort line, then it runs without errors.
$inputfile = "xxx/result.txt";
$builta = file_get_contents($inputfile);
$built =unserialize($builta);
$size = sizeof($built);
echo $size;
echo "<br><pre>";
print_r ($built);
echo "</pre>";
ksort($built);
echo "<br><pre>";
print_r ($built);
echo "</pre>";
Nothing complicated.
Update: after encountering more errors from dealing with large arrays, it appeared that the problem with ksort was due to not having enough memory to sort large arrays. The solution offered by tpaksu is a great workaround.
I assume your array’s name is $my_array; then you can use something like this:
But yes ksort would be better and simpler.
Edit : Sorry my bad. using sort as inline function was a mistake. Here’s the updated,tested and working code.