我有以下数组:
$arr = array(
array("2014-03-13"=>array("a"=>1,"b"=>2)),
array("2014-03-12"=>array("a"=>4,"b"=>3))
);
我希望改变它看起来更像这样的东西。
$arr = array(0=>array("date"=>"2014-03-13","a"=>1,"b"=>2),
1=>array("date"=>"2014-03-12","a"=>4,"b"=>3));
到目前为止我所拥有的。
$keys = array();
$vals = array();
foreach($arr as $row){
foreach($row as $key=>$val){
$keys[]=array("date"=>$key);
foreach($val as $keys=>$values){
$vals[]=array($keys=>$values);
}
}
}
获得array
的{{1}}工作正常,因此在下面的示例中,dates
有效,但$keys array
无法按预期工作,而是给我一个类似的数组对此。
$vals array
对于获得所需阵列的任何帮助表示赞赏。
答案 0 :(得分:1)
应该是
$result = array();
foreach($arr as $row) {
foreach($row as $date=>$values) {
$values['date'] = $date;
$result[] = $values;
}
}
答案 1 :(得分:1)
$arr = array(
array(
"2014-03-13" => array("a"=>1, "b"=>2)
),
array(
"2014-03-12" => array("a"=>4, "b"=>3)
),
);
$result = array();
foreach ($arr as $item) {
foreach ($item as $date => $values) {
$result[] = array_merge(array('date' => $date), $values);
}
}
var_dump($result);
答案 2 :(得分:0)
Here is the code:
$arr = array(
0=>array("2014-03-13"=>array("a"=>1,"b"=>2)),
array("2014-03-12"=>array("a"=>4,"b"=>3))
);
$finalarray=array();
foreach($arr as $k=>$v){
foreach($v as $kk=>$vv){
$newarray=array();
$newarray["date"]=$kk;
foreach($vv as $kkk=>$vvv){
$newarray[$kkk]=$vvv;
}
}
$finalarray[]=$newarray;
}
echo "<pre>";
print_r($finalarray);
echo "</pre>";
**Here is the output:**
Array
(
[0] => Array
(
[date] => 2014-03-13
[a] => 1
[b] => 2
)
[1] => Array
(
[date] => 2014-03-12
[a] => 4
[b] => 3
)
)