我已经从sql查询和数组push中构建了这个数组:
$months = array('January', 'February', 'March', 'April', 'May', 'Jun', 'July', 'August', 'September', 'October', 'November', 'December');
$monthPicked=array();//months added to result
$rows = array_map(function($item) use(&$monthPicked){
array_push($monthPicked, $item->month);
return (object) ['c' => [
(object) ['v' => $item->month, 'f' => null],
(object) ['v' => intval($item->jobcount), 'f' => null],
(object) ['v' => intval($item->interncount), 'f' => null]
]];
}, array_values($data));
$remainingMonths=array_diff($months, $monthPicked);
foreach($remainingMonths as $month)
{
$rows[]=(object) ['c' => [
(object) ['v' => $month, 'f' => null],
(object) ['v' => 0, 'f' => null],
(object) ['v' => 0, 'f' => null]
]];
}
现在,我需要按顺序安排从1月到12月的月份,因为array_push函数的月份名称是在早期数组的末尾插入的。
月份可以按顺序排列吗?
这是数组的输出:
Array ( [0] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => July [f] => ) [1] => stdClass Object ( [v] => 6 [f] => ) [2] => stdClass Object ( [v] => 2 [f] => ) ) ) [1] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => August [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 3 [f] => ) ) ) [2] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => January [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [3] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => February [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [4] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => March [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [5] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => April [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [6] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => May [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [7] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => Jun [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [8] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => September [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [9] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => October [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [10] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => November [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) [11] => stdClass Object ( [c] => Array (
[0] => stdClass Object ( [v] => December [f] => ) [1] => stdClass Object ( [v] => 0 [f] => ) [2] => stdClass Object ( [v] => 0 [f] => ) ) ) )
我尝试使用此功能,但它不适用于多个对象类数组。
usort($rows, "compare_months");
function compare_months($a, $b) {
$monthA = date_parse($a['month']);
$monthB = date_parse($b['month']);
return $monthA["month"] - $monthB["month"];
}
它说错误:致命错误:不能使用stdClass类型的对象作为数组
我想如何替换['month']以适应$ rows数组格式?