如何使用array_reverse反转数组元素的顺序?

时间:2019-07-10 11:01:12

标签: php

我正在尝试使用array_reverse反转数组的顺序,但输出结果仅为true,而不是数组的内容。 尝试时:

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

反转前的当前输出为:

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",…]

但是预期结果将是:

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"]

1 个答案:

答案 0 :(得分:0)

尝试一下

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

array_reverse可处理第一级数据 例如

 $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 ) )

所以只有第一级元素反转了。