我正在尝试填充数组,以便我可以使用chart.js
绘制图表我有一个看起来像这样的数组:
[0] => Array
(
[id] => 1
[date] => 09-04-2018
[length] => 10
)
[1] => Array
(
[id] => 2
[date] => 09-04-2018
[length] => 20
)
[2] => Array
(
[id] => 1
[date] => 10-04-2018
[length] => 11
)
[3] => Array
(
[id] => 2
[date] => 10-04-2018
[length] => 21
)
[4] => Array
(
[id] => 1
[date] => 11-04-2018
[length] => 12
)
[5] => Array
(
[id] => 1
[date] => 12-04-2018
[length] => 13
)
[6] => Array
(
[id] => 2
[date] => 12-04-2018
[length] => 23
)
[7] => Array
(
[id] => 1
[date] => 13-04-2018
[length] => 14
)
我需要将这个数组拆分为2个数据集(id 1和id 2)但是我不能在这些数据集中留有空白,因为chartJS不喜欢它。
我需要数组看起来像:
[1] => Array
(
[09-04-2018] => 10
[10-04-2018] => 11
[11-04-2018] => 12
[12-04-2018] => 13
[13-04-2018] => 14
)
[2] => Array
(
[09-04-2018] => 20
[10-04-2018] => 21
[11-04-2018] => 0
[12-04-2018] => 23
[13-04-2018] => 0
)
使用以下代码: -
foreach ($array as $item)
{
$id = $item['id'];
$date = $item['date'];
$length = $item['length'];
$output[$id][$date] = $length;
}
我可以按照我喜欢的格式生成一个数组,但由于初始数组缺少数据集2的2个日期,所以我没有添加这些数据,结果如下: -
[1] => Array
(
[09-04-2018] => 10
[10-04-2018] => 11
[11-04-2018] => 12
[12-04-2018] => 13
[13-04-2018] => 14
)
[2] => Array
(
[09-04-2018] => 20
[10-04-2018] => 21
[12-04-2018] => 23
)
有没有办法以我想要的方式填充数组,所以如果其中一个数据集使用该日期,两者(所有可能是3或4个数据集)都必须使用该日期,只需添加0?
答案 0 :(得分:1)
<强> INPUT 强>
$array = array(
array('id' => 1,'date' => '09-04-2018','length' => 10),
array('id' => 2,'date' => '09-04-2018','length' => 20),
array('id' => 1,'date' => '10-04-2018','length' => 11),
array('id' => 2,'date' => '10-04-2018','length' => 21),
array('id' => 1,'date' => '11-04-2018','length' => 12),
array('id' => 1,'date' => '12-04-2018','length' => 13),
array('id' => 2,'date' => '12-04-2018','length' => 23),
array('id' => 1,'date' => '13-04-2018','length' => 14)
);
<强>解强>
$id = array();
foreach($array as $r){
$data[$r['id']][$r['date']] = $r['length'];
if(!in_array($r['id'],$id)) $id[] = $r['id'];
}
for($i=0;$i<count($id);$i++){
$cid = $id[$i];
$nid = $id;
unset($nid[$i]);
foreach($data[$cid] as $k => $r){
foreach($nid as $n){
if(!isset($data[$n][$k]))$data[$n][$k] = 0;
}
}
}
echo "<pre>";print_r($data);
<强>输出强>
Array
(
[1] => Array
(
[09-04-2018] => 10
[10-04-2018] => 11
[11-04-2018] => 12
[12-04-2018] => 13
[13-04-2018] => 14
)
[2] => Array
(
[09-04-2018] => 20
[10-04-2018] => 21
[11-04-2018] => 0
[12-04-2018] => 23
[13-04-2018] => 0
)
)
答案 1 :(得分:1)
此解决方案中,如果id
然后会添加date
0
值
$a = array(
array('id' => '1','date' => '09-04-2018','length' => '10'),
array('id' => '2','date' => '09-04-2018','length' => '20'),
array('id' => '1','date' => '10-04-2018','length' => '11'),
array('id' => '2','date' => '10-04-2018','length' => '21'),
array('id' => '1','date' => '11-04-2018','length' => '12'),
array('id' => '1','date' => '12-04-2018','length' => '13'),
array('id' => '2','date' => '12-04-2018','length' => '23'),
array('id' => '1','date' => '13-04-2018','length' => '14')
);
$b = array_unique(array_column($a,'date'));
$a1 = $a2 = [];
foreach ($b as $key => $date) {
$a1[1][$date] = 0;
$a1[2][$date] = 0;
foreach ($a as $key2 => $value2) {
if($value2['id'] == 1){
if($date == $value2['date']){
$a1[1][$date] = $value2['length'];
}
}elseif($value2['id'] == 2){
if($date == $value2['date']){
$a1[2][$date] = $value2['length'];
}
}
}
}
适用于2个以上的数据集
$b = array_unique(array_column($a,'date'));
$sets = array_unique(array_column($a,'id'));
$a1 = $a2 = [];
foreach ($sets as $s) {
foreach ($b as $key => $date) {
$a1[$s][$date] = 0;
foreach ($a as $key2 => $value2) {
if($value2['id'] == $s){
if($date == $value2['date']){
$a1[$s][$date] = $value2['length'];
}
}
}
}
}
echo "<pre>";
print_r($a1);
echo "</pre>";
<强>输出强>
Array
(
[1] => Array
(
[09-04-2018] => 10
[10-04-2018] => 11
[11-04-2018] => 12
[12-04-2018] => 13
[13-04-2018] => 14
)
[2] => Array
(
[09-04-2018] => 20
[10-04-2018] => 21
[11-04-2018] => 0
[12-04-2018] => 23
[13-04-2018] => 0
)
)
答案 2 :(得分:0)
只需添加一个条件即可获得
$data_set = array();
foreach ($array as $item)
{
$data_set[$item['id']][$item['date']] = (($item['length'] == 0)?0:$item['length']);
}
echo '<pre>'; print_r($data_set); echo '</pre>';exit();
答案 3 :(得分:0)
<?php
$dataset=array();
for ($i=0;$i<=10;$i++){
for ($k=0;$k<=100;$k++){
$int= mt_rand(time()-1000000,time());
$dataset[$i][date("Y-m-d",$int)] = mt_rand(0,30) ;
}
}
print_r($dataset);
你会得到像
这样的东西Array
(
[0] => Array
(
[2018-04-16] => 19
[2018-04-11] => 24
[2018-04-09] => 21
[2018-04-10] => 7
[2018-04-06] => 19
[2018-04-12] => 11
[2018-04-14] => 3
[2018-04-07] => 19
[2018-04-15] => 20
[2018-04-13] => 2
[2018-04-17] => 29
[2018-04-08] => 0
)
[1] => Array
(
[2018-04-09] => 12
[2018-04-13] => 13
[2018-04-11] => 23
[2018-04-06] => 28
[2018-04-07] => 24
[2018-04-15] => 26
[2018-04-08] => 19
[2018-04-12] => 24
[2018-04-17] => 24
[2018-04-16] => 26
[2018-04-14] => 17
[2018-04-05] => 24
[2018-04-10] => 17
)
[2] => Array
(
[2018-04-14] => 29
[2018-04-08] => 25
[2018-04-15] => 17
[2018-04-11] => 30
[2018-04-06] => 0
[2018-04-10] => 9
[2018-04-12] => 7
[2018-04-13] => 4
[2018-04-07] => 23
[2018-04-16] => 22
[2018-04-09] => 12
[2018-04-17] => 14
)
[3] => Array
(
[2018-04-10] => 22
[2018-04-13] => 14
[2018-04-11] => 6
[2018-04-16] => 5
[2018-04-15] => 27
[2018-04-14] => 21
[2018-04-09] => 2
[2018-04-06] => 5
[2018-04-08] => 20
[2018-04-12] => 6
[2018-04-17] => 6
[2018-04-05] => 5
[2018-04-07] => 23
)
[4] => Array
(
[2018-04-06] => 20
[2018-04-05] => 20
[2018-04-09] => 5
[2018-04-07] => 25
[2018-04-15] => 12
[2018-04-13] => 22
[2018-04-10] => 3
[2018-04-16] => 11
[2018-04-11] => 16
[2018-04-14] => 5
[2018-04-12] => 7
[2018-04-17] => 25
[2018-04-08] => 12
)
[5] => Array
(
[2018-04-07] => 24
[2018-04-15] => 3
[2018-04-12] => 13
[2018-04-06] => 18
[2018-04-09] => 4
[2018-04-08] => 2
[2018-04-10] => 2
[2018-04-11] => 14
[2018-04-16] => 17
[2018-04-13] => 0
[2018-04-14] => 25
[2018-04-17] => 30
[2018-04-05] => 20
)
[6] => Array
(
[2018-04-06] => 5
[2018-04-16] => 6
[2018-04-13] => 18
[2018-04-08] => 5
[2018-04-11] => 11
[2018-04-15] => 18
[2018-04-12] => 8
[2018-04-14] => 23
[2018-04-05] => 0
[2018-04-17] => 22
[2018-04-09] => 27
[2018-04-10] => 25
[2018-04-07] => 19
)
[7] => Array
(
[2018-04-07] => 2
[2018-04-15] => 6
[2018-04-16] => 24
[2018-04-08] => 24
[2018-04-09] => 0
[2018-04-10] => 21
[2018-04-11] => 26
[2018-04-13] => 4
[2018-04-12] => 25
[2018-04-06] => 16
[2018-04-14] => 17
[2018-04-05] => 13
[2018-04-17] => 16
)
[8] => Array
(
[2018-04-10] => 6
[2018-04-16] => 2
[2018-04-12] => 7
[2018-04-15] => 15
[2018-04-07] => 25
[2018-04-09] => 17
[2018-04-08] => 25
[2018-04-06] => 4
[2018-04-11] => 29
[2018-04-14] => 6
[2018-04-13] => 15
[2018-04-17] => 28
[2018-04-05] => 20
)
[9] => Array
(
[2018-04-09] => 2
[2018-04-11] => 14
[2018-04-14] => 20
[2018-04-07] => 3
[2018-04-13] => 23
[2018-04-17] => 3
[2018-04-16] => 16
[2018-04-15] => 2
[2018-04-06] => 27
[2018-04-12] => 5
[2018-04-10] => 26
[2018-04-08] => 3
)
[10] => Array
(
[2018-04-07] => 20
[2018-04-11] => 28
[2018-04-14] => 4
[2018-04-05] => 13
[2018-04-16] => 0
[2018-04-09] => 27
[2018-04-08] => 22
[2018-04-06] => 30
[2018-04-12] => 14
[2018-04-13] => 5
[2018-04-15] => 18
[2018-04-17] => 4
[2018-04-10] => 21
)
)