如何将此代码简化为循环?我是否也需要在循环中使用foreach
循环?
foreach ($data as $item) {
$a = new stdClass;
$a->v = $item->location_name;
$a->f = null;
$b = new stdClass;
$b->v = intval($item->count);
$b->f = null;
$c = new stdClass;
$c->c = array($a, $b);
$rows[] = $c;
}
答案 0 :(得分:0)
看起来你可以更简单地使用array_map
和一些数组到对象的转换
$rows = array_map(function($item) {
return (object) ['c' => [
(object) ['v' => $item->location_name, 'f' => null],
(object) ['v' => intval($item->count), 'f' => null]
]];
}, array_values($data));