i need reverse order of values in array:
// current array looks // 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 invert values keeping keys should 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 have tried without success:
<?php $newarray= array_reverse($array, true); echo json_encode($newarray); /* above code output (note 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, tried following without success:
<?php $k = array_keys($array); $v = array_values($array); $rv = array_reverse($v); $newarray = array_combine($k, $rv); echo json_encode($b); /* 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 time.
haven't tested it, should it:
$newarray = array(); foreach($array $key => $val) { $newarray[$key] = array_reverse($val); }
Comments
Post a Comment