我正在开发php应用程序。我使用Google Chart API来显示图表。 我已选择并返回图表所需的数据。 我得到了以下数组作为输出。
print_r($output);
//Out put
Array
(
[0] => Array
(
[month] => April
[sec_id] => 2
[sec_name] => Commerce
[count] => 1
)
[1] => Array
(
[month] => June
[sec_id] => 2
[sec_name] => Commerce
[count] => 3
)
[2] => Array
(
[month] => July
[sec_id] => 2
[sec_name] => Commerce
[count] => 1
)
[3] => Array
(
[month] => August
[sec_id] => 4
[sec_name] => Science
[count] => 3
)
[4] => Array
(
[month] => August
[sec_id] => 3
[sec_name] => Maths
[count] => 2
)
[5] => Array
(
[month] => August
[sec_id] => 1
[sec_name] => Art
[count] => 2
)
[6] => Array
(
[month] => August
[sec_id] => 2
[sec_name] => Commerce
[count] => 2
)
)
print_r(json_encode($output)); // return above array as output
我使用ajax请求上面的数据(数据类型是JSON) 我想将数据返回以生成谷歌图表。
[
['Month', 'Art', 'Commerce', 'Maths', 'Sience'],
['April', '', 2, '', ''],
['June', '', 3, '', ''],
['July', '', 1, '', ''],
['August', 2, 2, 3, 3]
]
我试过这段代码
$output = array();
$output[0] = array('Month', 'Art', 'Commerce', 'Maths', 'Science');
foreach($records as $key=> $record){
$art =''; $commerce =''; $maths=''; $science='';
if($record['sec_id'] == 1){
$art = $record['count'];
}else if($record['sec_id'] == 2){
$commerce = $record['count'];
}else if($record['sec_id'] == 3){
$maths = $record['count'];
}else if($record['sec_id'] == 4){
$science = $record['count'];
}
$output[++$key] = array(0 => $record['month'], 1 => $art, 2 => $commerce, 3 => $maths, 4 => $science);
}
function super_unique($array){
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value){
if ( is_array($value)){
$result[$key] = super_unique($value);
}
}
return $result;
}
$output = super_unique($output);
输出
[["Month","Art","Commerce","Maths","Science"],["April","","1"],["June","","3"],["July","","1"],{"0":"August","1":"","4":"3"},{"0":"August","1":"","3":"2"},["August","2",""],["August","","2"]]
答案 0 :(得分:0)
让我们重新考虑你的算法。当你浏览记录中的每个元素时,我们需要问两件事:我们提取的数据是什么以及我们想用它做什么?
第一个问题很简单:我们只是提取“计数”值。
第二个问题是确定我们的算法。我们想要做的是获取'count'值,并将它贴在我们的ourpur数组中的特定位置。
将输出数组视为一个表,我们可以看到'count'的所需位置由'month'字段(确定行)和'sec_id'/'sec_name'字段确定(确定列)。所以你希望你的循环看起来像是......
foreach($records as $record)
{
$output[$record['month']][$record['sec_id']] = $record['count']
}
对此的第一个警告是,对于每个唯一月份,您仍然需要创建和初始化子数组,并且您必须只执行一次。所以,循环变为。
foreach($records as $record)
{
if(!is_array($output[$record['month']]))
$output[$record['month']] = array(0 => $record['month'], 1 => '', 2 => '', 3 => '', 4 => '');
$output[$record['month']][$record['sec_id']] = $record['count']
}
最后,我们使用实际月份名称作为顶级数组中的键。为了符合所需输出中指定的仅数字键,我们可以使用以下代码丢弃这些键。
$output = array_values($output)
如果我认为你正在尝试使用super_unique()来将行与同一个月组合在一起,那就完全不是它所做的了。首先,array_unique()不组合行,它消除了重复。由于您在比较序列化行,而不是仅查看月份字段,因此没有任何行是重复的,因此该函数无效。此外,由于您的几个数组字段设置为'',序列化/反序列化过程实际上导致这些字段被删除,这就是为什么您最终得到少于五个元素的子数组和关联键。
答案 1 :(得分:0)
循环和重组非常简单,特别是因为你的sec_id
与数组索引很好地匹配。
$temp = array();
$output = array(
array('Month', 'Art', 'Commerce', 'Maths', 'Science')
);
foreach ($records as $record) {
$month = $record["month"];
$sec_id = $record["sec_id"];
$count = $record["count"];
if (!isset($temp[$month])) {
$temp[$month] = array_fill(0, 5, '');
}
$temp[$month][0] = $month;
$temp[$month][$sec_id] += $count;
}
$output = array_merge($output, array_values($temp));
echo json_encode($output);
[["Month","Art","Commerce","Maths","Science"],["April","",2,"",""],["June","",3,"",""],["July","",1,"",""],["August",2,2,2,3]]