我有一个学说查询
$qb->select(['i.name', 'io.year', 'count(io) AS population'])
->from('AppBundle:IOxxx', 'io')
->innerJoin('AppBundle:Iyyy', 'i', 'WITH' , 'io.country = i.id')
->groupBy('i.name')
->addGroupBy('io.year');
返回一个数组,其中包含按国家名称和年份分组的数据(在这种情况下,我现在只有一个国家/地区,但只会添加其他国家/地区);
array (size=6)
0 =>
array (size=3)
'name' => string 'Africa' (length=6)
'year' => string '1960'
'population' => int 133
1 =>
array (size=3)
'name' => string 'Africa' (length=6)
'year' => string '1970'
'population' => int 203
2 =>
array (size=3)
'name' => string 'Africa' (length=6)
'year' => string '1980'
'population' => int 298
3 =>
array (size=3)
'name' => string 'Africa' (length=6)
'year' => string '1990'
'population' => int 378
4 =>
array (size=3)
'name' => string 'Africa' (length=6)
'year' => string '2000'
'population' => int 450
5 =>
array (size=3)
'name' => string 'Africa' (length=6)
'year' => string '2010'
'population' => int 560
我正在尝试从这个数组创建两个数组,一个包含Countries,另一个包含剩余的数据;
countries数组应该如下所示(使用名称);
$countries = ['Africa', 'AnotherCountry', 'MoreCountries'];
其余数据应如下所示;
$series = [
['name' => '1960', 'data' => [133, 11, 22]],
['name' => '1970', 'data' => [203, 11, 22]],
['name' => '1980', 'data' => [298, 11, 22]],
['name' => '1990', 'data' => [378, 11, 22]],
['name' => '2000', 'data' => [450, 11, 22]],
['name' => '2010', 'data' => [560, 11, 22]],
];
数据中的第一个值与国家/地区中的第一个国家/地区相对应,等等......
我试图将这个组织起来用于图形目的,就像你在高例中的例子中看到的那样:
https://www.highcharts.com/demo/bar-basic(然后点击'查看选项'看看它是如何配置的)
有人可以帮帮我吗?非常感谢。
用我尝试的方式更新:
我试图经历一个循环:
$countries = [];
$series = [];
foreach ($result as $item) {
$countries[] = $item['name'];
$series[]['name'] = $item['status'];
$series[]['data'][] = $item['number_of_orders'];
}
但显然现在我把所有的名字都聚集在一起,比如非洲6次,但我只需要一次。
array (size=6)
0 => string 'Africa' (length=6)
1 => string 'Africa' (length=6)
2 => string 'Africa' (length=6)
3 => string 'Africa' (length=6)
4 => string 'Africa' (length=6)
5 => string 'Africa' (length=6)
对于我得到的系列:
array (size=14)
0 =>
array (size=1)
'name' => string '1960' (length=8)
1 =>
array (size=1)
'data' =>
array (size=1)
0 => int 133
代替'数据'紧挨着' name'在第一个数组而不是它自己的数组...
答案 0 :(得分:1)
可以使用两个这样的循环来完成(:host /deep/ router-outlet + * {height: 100%}
是您的查询结果):
$data
您需要变量$years = [];
$countries = [];
foreach ($data as $row) {
if (!in_array($row['name'], $countries)) {
$countries[] = $row['name'];
}
$years[$row['year']][$row['name']] = $row['population'];
}
$result = [];
foreach ($years as $year => $data) {
$row = [
'name' => $year,
'data' => array_fill(0, count($countries), 0)
];
foreach ($data as $country => $population) {
$row['data'][array_search($country, $countries)] = $population;
}
$result[] = $row;
}
和$countries