Trying to understand this code in a book, that is to sort nested associative arrays. But, it is not sorting the nested arrays correctly.
In the book, it’s using pass by reference. Since pass by reference is deprecated in the current PHP, I have actually tried setting the allow_call_time_pass_reference in the php.ini from the default of “off” to “on”, and have also tried return by reference, as in the code below, both giving the same incorrect results.
What am I missing?
Thank you in advance
<pre>
<?php
function sortNestedArrayAssoc(&$a) {
ksort($a);
foreach ($a as $key => $value) {
if (is_array($value)) {
sortNestedArrayAssoc($value);
}
}
}
$arr = array(
'Roman' =>
array('one' => 'I', 'two' => 'II', 'three' => 'III', 'four' => 'IV'),
'Arabic' =>
array('one' => '1', 'two' => '2', 'three' => '3', 'four' => '4')
);
sortNestedArrayAssoc($arr);
print_r($arr);
?>
</pre>
This is what the code is supposed to produce
Array
(
[Arabic] => Array
(
[four] => 1
[one] => 2
[three] => 3
[two] => 4
)
[Roman] => Array
(
[four] => I
[one] => II
[three] => III
[two] => IV
)
)
This is what it’s actually producing
Array
(
[Arabic] => Array
(
[one] => 1
[two] => 2
[three] => 3
[four] => 4
)
[Roman] => Array
(
[one] => I
[two] => II
[three] => III
[four] => IV
)
)