我得到了以下数组:
["positive"]=>
array(3) {
[0]=>
array(2) {
[0]=>
int(1473022800000) //unix timestamp
[1]=>
int(100)
}
[1]=>
array(2) {
[0]=>
int(1473109200000)
[1]=>
int(200)
}
[2]=>
array(2) {
[0]=>
int(1473195600000)
[1]=>
int(300)
}// etc...
["neutral"]=>
array(3) {
[0]=>
array(2) {
[0]=>
int(1473022800000) //same day as with positive
[1]=>
int(400)
}
[1]=>
array(2) {
[0]=>
int(1473109200000)//also second same day as with positive
[1]=>
int(500)
}
[2]=>
array(2) {
[0]=>
int(1473195600000)
[1]=>
int(600)
} // etc...
我需要重新打包以获得以下内容:
["1473022800000"]=>
array(2) {
['positive']=> (int)500 //example values, should be calculated
['neutral'] => (int)200
}
["1473109200000"]=>
array(2) {
['positive']=> (int)500
['neutral'] => (int)200
}
因此,我需要将其重新包装,不是来自正面或中立,而是来自日期以便以这种方式总结。任何有关如何使用PHP的建议都将受到欢迎。谢谢。
UPD 这是我到目前为止所尝试的内容
foreach($myarray as $key=>$val){ //getting the dates
//var_dump($key);exit;
for($i=0;$i<=30;++$i){
$dates[] = $val[$i][0];
}
}
$dates = array_unique(array_values($dates));
$dates_upd = [];
foreach($dates as $date) {
foreach($myarray as $key=>$val){
for($i=0;$i<=30;++$i){
if($val[$i][0]==$date){
$dates_upd['date'] = $date;
$dates_upd['date']['total'] = $val[$i][1];//getting errors here (cant use a scalar value etc...)
}
}
}
}
答案 0 :(得分:0)
您可以使用几个嵌套的foreach循环来执行此操作:
$out=[];
foreach($myarray as $key=>$subarray){
foreach($subarray as $item){
$date = $item[0];
$value = $item[1];
$out[$date][$key]=$value;
}
}