我目前正在处理phplot,我想在其中添加一行几行。
所以这就是问题所在,我想说我有一系列数据:
$graph->SetDataType('data-data');
$data = array(
array("",0,37.0023239,34.4618,31.92128),
array("",1,39.6116079,37.2759,34.94019),
array("",2,41.4738623,39.1285,36.78314)
);
现在我有另一个数组,让我们把它称为a,其中键实际上是我想把它放在数据数组中的地方:
$a = array (
"0"=>'38';
"2"=>'40';
)
我希望数据将它们插入到相应的x值中。所以最后,我想要这样的东西:
$data = array(
array("",0,37.0023239,34.4618,31.92128,38),
array("",1,39.6116079,37.2759,34.94019,''),
array("",2,41.4738623,39.1285,36.78314,40)
);
有可能这样做吗?任何建议都将非常感谢。
答案 0 :(得分:2)
应该相当简单:
foreach($data as $key => &$val) {
if(isset($a[$key])) {
$val[] = $a[$key];
} else {
$val[] = '';
}
}
答案 1 :(得分:2)
与上一个答案几乎相同,但是内部向外翻转:迭代数据数组并检查每个键值对是否$a
包含相应的元素;是:追加它,否:追加一个空字符串。
<?php
$data = array(
array("",0,37.0023239,34.4618,31.92128),
array("",1,39.6116079,37.2759,34.94019),
array("",2,41.4738623,39.1285,36.78314)
);
$a = array (
"0"=>'38',
"2"=>'40',
);
foreach($data as $k=>&$v) {
$v[] = isset($a[$k]) ? $a[$k] : '';
}
var_export($data);
打印
array (
0 =>
array (
0 => '',
1 => 0,
2 => 37.0023239,
3 => 34.461799999999997,
4 => 31.921279999999999,
5 => '38',
),
1 =>
array (
0 => '',
1 => 1,
2 => 39.611607900000003,
3 => 37.2759,
4 => 34.940190000000001,
5 => '',
),
2 =>
array (
0 => '',
1 => 2,
2 => 41.4738623,
3 => 39.128500000000003,
4 => 36.783140000000003,
5 => '40',
),
)