简体   繁体   中英

How to reverse the order of array elements using array_reverse?

I'm trying to reverse an array's order using array_reverse but the output result is just true not the content of the array. While trying:

$response = array( 'labels' => $dataFormats[0], 'datasets' => $datesets );
$result = array_reverse($response); return $result;

The current output before reversing is:

datasets: [,…]
0: {label: "TOTAL", data: [14, 839, 12396, 16765, 17809, 15961, 16735, 17155, 16989, 18742, 16243, 16742],…}
data: [14, 839, 12396, 16765, 17809, 15961, 16735, 17155, 16989, 18742, 16243, 16742]
labels: ["Jul-2019", "Jun-2019", "May-2019", "Apr-2019", "Mar-2019", "Feb-2019", "Jan-2019", "Dec-2018",…]

But the expecting result will be:

data: [16742, 16243, 18742, 16989,17155, 16735, 15961, 17809, 16765, 12396, 839,14]
labels: [ "Aug-2018", "Sep-2018", "Oct-2018" ,  "Nov-2018", "Dec-2018",  "Jan-2019", "Feb-2019", "Mar-2019", "Apr-2019", "Jun-2019",  "Jul-2019"]

Try this

$response = array( 'labels' => array_reverse($dataFormats[0]), 'datasets' => array_reverse($datesets) );

array_reverse works on first level of data eg

 $a = array(1=>array(10,12,45,5), 2=>array(34,53,12))
output: Array ( [0] => Array ( [0] => 34 [1] => 53 [2] => 12 ) [1] => Array ( [0] => 10 [1] => 12 [2] => 45 [3] => 5 ) )

so only first level of element reversed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM