$a =\x0d\x0aArray ( '1969-12-31' =10, '1969-12-30' = 20,'1969-12-29' = 5, \x0d\x0a'1969-12-28' = 6, '1969-12-27' = 30, '1969-12-26' = 70, \x0d\x0a'1969-12-25'= 33);\x0d\x0a//转换成这样的二维数组: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]... )\x0d\x0a$b = Array();\x0d\x0aforeach ($a as $key = $value) {\x0d\x0a $b[]=Array('day'=$key,'sum'=$value);\x0d\x0a}
站在用户的角度思考问题,与客户深入沟通,找到云冈网站设计与云冈网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、做网站、成都外贸网站建设公司、企业官网、英文网站、手机端网站、网站推广、主机域名、网页空间、企业邮箱。业务覆盖云冈地区。
例如:
用php递归将二维数组转换成一维数组:
函数原形:array array_multi2single(array)
实现功能:将多维数组的数值存放到一维数组中,不保存Key。
?php
function array_multi2single($array){
static $result_array=array();
foreach($array as $value){
if(is_array($value)){
array_multi2single($value);
}
else
$result_array[]=$value;
}
return $result_array;
}
$array=array("1"=array("A","B","C",array("D","E")),"2"=array("F","G","H","I"));
$array=array_multi2single($array);
foreach($array as $value){
echo "h5$value/h5";
echo "br";
}
?
可用于多维数组.
//根据你的需求,我写了个测试程序,你整理下就可以用了
$array = array(
array('id'=1,"parentsid"=0),
array('id'=2,"parentsid"=0),
array('id'=3,"parentsid"=2),
array('id'=4,"parentsid"=2),
array('id'=5,"parentsid"=1),
array('id'=6,"parentsid"=1)
);
$newArray = array();
foreach($array as $row){
if($row['parentsid'] == 0){
$newArray[] = $row;
}
}
foreach($array as $row){
foreach($newArray as $key=$newRow){
if($row['parentsid'] == $newRow['id']){
$newArray[$key]['subnav'][] = $row;
}
}
}
?php
$a = array('a0'='0001','b0'='2','a1'='0003','b1'='6');
$array = change($a,1);
print_r($array);
function change($array,$num){
$b = array();
for($i=0;$i=$num;$i++){
$array = array();
$array['id'] = empty($a['a'.$i]) ? "" : $a['a'.$i];
$array['num'] = empty($a['b'.$i]) ? "" : $a['b'.$i];
$b[] = $array;
}
return $b;
}
?
基本实现了你所要的功能