php 将一维数组转换成二维数组
一维数组:Array([1969-12-31]=>10[1969-12-30]=>20[1969-12-29]=>5[1969-12-28]=>6[1969-12-27]...
一维数组:Array ( [1969-12-31] =>10 [1969-12-30] => 20 [1969-12-29] => 5
[1969-12-28] => 6 [1969-12-27] => 30 [1969-12-26] => 70 [1969-12-25]
=> 33)
转换成这样的二维数组:Array ( [0] => Array ( [day] => 1969-12-31 [sum] => 10 ) [1] => Array ( [day] => 1969-12-30 [sum] => 20 ) [2] => Array ( [day] => 1969-12-29 [sum] => 5 ) [3]... )
怎么转?
一维数组的个数不确定 展开
[1969-12-28] => 6 [1969-12-27] => 30 [1969-12-26] => 70 [1969-12-25]
=> 33)
转换成这样的二维数组:Array ( [0] => Array ( [day] => 1969-12-31 [sum] => 10 ) [1] => Array ( [day] => 1969-12-30 [sum] => 20 ) [2] => Array ( [day] => 1969-12-29 [sum] => 5 ) [3]... )
怎么转?
一维数组的个数不确定 展开
1个回答
展开全部
$a =
Array ( '1969-12-31' =>10, '1969-12-30' => 20,'1969-12-29' => 5,
'1969-12-28' => 6, '1969-12-27' => 30, '1969-12-26' => 70,
'1969-12-25'=> 33);
//转换成这样的二维数组:Array ( [0] => Array ( [day] => 1969-12-31 [sum] => 10 ) [1] => Array ( [day] => 1969-12-30 [sum] => 20 ) [2] => Array ( [day] => 1969-12-29 [sum] => 5 ) [3]... )
$b = Array();
foreach ($a as $key => $value) {
$b[]=Array('day'=>$key,'sum'=>$value);
}
Array ( '1969-12-31' =>10, '1969-12-30' => 20,'1969-12-29' => 5,
'1969-12-28' => 6, '1969-12-27' => 30, '1969-12-26' => 70,
'1969-12-25'=> 33);
//转换成这样的二维数组:Array ( [0] => Array ( [day] => 1969-12-31 [sum] => 10 ) [1] => Array ( [day] => 1969-12-30 [sum] => 20 ) [2] => Array ( [day] => 1969-12-29 [sum] => 5 ) [3]... )
$b = Array();
foreach ($a as $key => $value) {
$b[]=Array('day'=>$key,'sum'=>$value);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询