gpt4 book ai didi

php - 如何在 PHP 的关联数组中将行创建为列名

转载 作者:行者123 更新时间:2023-11-29 06:17:01 25 4
gpt4 key购买 nike

我想在 PHP 中将行转换为关联数组中的列名。我的数据以关联数组格式存储在单个变量 $salesone 中。

下面是存储在变量$salesone中的数据。

[{"Datetime":"2011-01-31", "Make":"Datsun", "Sales":0},
{"Datetime":"2011-02-28", "Make":"Datsun", "Sales":0},
{"Datetime":"2011-03-31", "Make":"Datsun", "Sales":0},
{"Datetime":"2011-01-31", "Make":"Fiat", "Sales":2150},
{"Datetime":"2011-02-28", "Make":"Fiat", "Sales":1839},
{"Datetime":"2011-03-31", "Make":"Fiat", "Sales":1860},
{"Datetime":"2011-01-31", "Make":"Ford", "Sales":10026},
{"Datetime":"2011-02-28", "Make":"Ford", "Sales":9293},
{"Datetime":"2011-03-31", "Make":"Ford", "Sales":10485}]

但谷歌折线图无法将其解释为绘制折线图。它给我以下错误:

'all the data on given axis must be of same data type'


我想要以下格式的数据:

[{"Datetime":"2011-01-31", "Datsun":0, "Fiat":2150, "Ford":10026},
{"Datetime":"2011-02-28", "Datsun":0, "Fiat":1839, "Ford":9293},
{"Datetime":"2011-01-31", "Datsun":0, "Fiat":1860, "Ford":10845}]

如何在 PHP 中以编程方式转换此数据? (它作为关联数组存储在 $salesone 变量中)。

最佳答案

请使用以下代码输出:

<?php
$number = '[{"Datetime":"2011-01-31","Make":"Datsun","Sales":0},
{"Datetime":"2011-02-28","Make":"Datsun","Sales":0},
{"Datetime":"2011-03-31","Make":"Datsun","Sales":0},
{"Datetime":"2011-01-31","Make":"Fiat","Sales":2150},
{"Datetime":"2011-02-28","Make":"Fiat","Sales":1839},
{"Datetime":"2011-03-31","Make":"Fiat","Sales":1860},
{"Datetime":"2011-01-31","Make":"Ford","Sales":10026},
{"Datetime":"2011-02-28","Make":"Ford","Sales":9293},
{"Datetime":"2011-03-31","Make":"Ford","Sales":10485}]';

$number = json_decode($number);
$output = array();
foreach($number as $value){
$output[$value->Datetime]['Datetime'] = $value->Datetime;
$output[$value->Datetime][$value->Make] = $value->Sales;
}
$output = array_values($output);
$output = json_encode($output);
echo $output;

请使用下面的代码进行下一次评论:

<?php
$number = '[{"Datetime":"2011-01-31","Make":"Datsun","Sales":0},
{"Datetime":"2011-02-28","Make":"Datsun","Sales":0},
{"Datetime":"2011-03-31","Make":"Datsun","Sales":0},
{"Datetime":"2011-01-31","Make":"Fiat","Sales":2150},
{"Datetime":"2011-02-28","Make":"Fiat","Sales":1839},
{"Datetime":"2011-03-31","Make":"Fiat","Sales":1860},
{"Datetime":"2011-01-31","Make":"Ford","Sales":10026},
{"Datetime":"2011-02-28","Make":"Ford","Sales":9293},
{"Datetime":"2011-03-31","Make":"Ford","Sales":10485}]';

$number = json_decode($number);
$output = array();
foreach($number as $value){
$output[$value->Datetime]['Datetime'] = $value->Datetime;
$output[$value->Datetime][$value->Make] = $value->Sales;
}
$output = array_values($output);

$newOutput = array();
$counter = 0;
$colCounter = 0;
$colArray = array();
foreach($output as $key=>$value){
foreach($value as $innerKey => $innerValue){
if(!array_key_exists($innerKey,$colArray)){
$colArray[$innerKey] = $innerKey;
$newOutput['cols'][$colCounter]['label'] = $innerKey;
$newOutput['cols'][$colCounter]['type'] = ($innerKey == "Datetime")?"date":"string";

$colCounter++;
}
$newOutput['rows'][$counter]['c'][]['v'] = ($innerKey == "Datetime")?"date(".date('Y,n,j',$innerValue).")":$innerValue;
}
$counter++;
}
$newOutput = json_encode($newOutput);
echo $newOutput;
?>

我认为这对你有帮助

关于php - 如何在 PHP 的关联数组中将行创建为列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35307674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com