I need to reverse the order of the values in array:
// My current array looks like this
// echo json_encode($array);
{ "t" : [ 1, 2, 3, 4, 5, 6 ],
"o" : [ 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 ],
"h" : [ 1.2, 2.2, 3.2, 4.2, 5.2, 6.2 ]
}
I need to invert the values keeping the keys so it should look like this:
// echo json_encode($newArray);
{ "t" : [6, 5, 4, 3, 2, 1 ],
"o" : [ 6.6, 5.5, 4.4, 3.3, 2.2, 1.1],
"h" : [ 6.2, 5.2, 4.2, 3.2, 2.2, 1.2 ]
}
What I have tried without success:
<?php
$newArray= array_reverse($array, true);
echo json_encode($newArray);
/* The above code output (Note that the values keep order):
{
"h":[1.2, 2.2, 3.2, 4.2, 5.2, 6.2]
"o":[1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
"t":[1,2,3,4,5,6]
}
*/
?>
After reading similar question here, also tried the following but without success:
<?php
$k = array_keys($array);
$v = array_values($array);
$rv = array_reverse($v);
$newArray = array_combine($k, $rv);
echo json_encode($b);
/* The above code change association of values = keys, output:
{ "h": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6],
"o": [1,2,3,4,5,6],
"t": [1.2, 2.2, 3.2, 4.2, 5.2, 6.2]
}
?>
Thank you very much for your time.