php 多维数组转换
把字符类型转换成时间类型的,先用foreach遍历出来时间,在通过时间函数转换成时间格式;
例子:
<?php
echo "<pre>";
$lsrfield = array(
0 =>array(
'lsrtime' => '1410710400',
'lsrqian' => '完善个人资料奖励积分',
),
1 =>array(
'lsrtime' => '2410710400',
'lsrqian' => '完善个人资料奖励积分',
),
2 =>array(
'lsrtime' => '3410710400',
'lsrqian' => '完善个人资料奖励积分',
)
);
print_r($lsrfield);
foreach($lsrfield as $k => $val){
$lsrfield[$k]['lsrtime'] = date('Y-m-d H:i:s',$val['lsrtime']);
}
print_r($lsrfield);
?>
通过转换出的输出的结果是:
Array ( [0] => Array ( [lsrtime] => 2014-09-14 16:00:00 [lsrqian] => 完善个人资料奖励积分 ) [1] => Array ( [lsrtime] => 1910-04-17 11:18:24 [lsrqian] => 完善个人资料奖励积分 ) [2] => Array ( [lsrtime] => 1941-12-24 13:05:04 [lsrqian] => 完善个人资料奖励积分 ) )
通过上面的例子,我们可以举一反三,可以用同样的方法可转化打其他的字符类型;
通常在返回数据的时候,我们的数据是通过两个表查询或者是多个表查询的结果,但是并不是我们想要的结果数组,所以我们要对数组进行处理,如此才能得到我们想要的结果;
比如说:
例子:如果两个数据组中from_userid相同时就保留$select中的数,不相同时,两个数组的数据都不保留?
这是一个数组
$select=Array( [0] => Array ( [id] => 1048 [from_userid] => 0 [thru_userid] => 1243 [addtime] => 2015-01-11 13:11:15 [num] => 11 ) [1] => Array ( [id] => 1832 [from_userid] => 56 [thru_userid] => 57 [addtime] => 2014-12-20 16:42:45 [num] => 1 )
[2] => Array ( [id] => 561 [from_userid] => 728 [thru_userid] => 729 [addtime] => 2015-01-05 16:47:02 [num] => 1 )
}
$count2=Array( [0] => Array( [id] => 1009 [from_userid] => 1201 [thru_userid] => 1202 [addtime] => 2015-01-09 18:21:39 ) [1] => Array( [id] => 561 [from_userid] => 728 [thru_userid] => 729 [addtime] => 2015-01-05 16:47:02 ) )
下面是处理合并数据组的代码
$n=0; //定义新的数组重零开始排序
$newResult=array(); //定义一个数组
for($i=0;$i<count($result);$i++){
for($j=0;$j<count($count2);$j++){
if($result[$i]['from_userid']==$count2[$j]['from_userid']){ //判断是否相等
$newResult[$n]=$result[$i]; //定义一个数组来重新接收符合条件的数组
$n++;
break;
}
}
}
输出的结果是:
Array ( [0] => Array ( [id] => 561 [from_userid] => 728 [thru_userid] => 729 [addtime] => 2015-01-05 16:47:02 [num] => 1 )
}
总结:两个数组通过form_userid相同的而关联在一起的!所以如果form_userid相同的时候,只保留$select的数据,并把数据放到新数组$newResult当中去,如果form_userid不相同的时候,就去除去,这样就能得到我们想要的数据
例子2:在多维数组添加一个元素?
for($i=0;$i<count($select);$i++){
$select[$i]['num']=0;
}
用for循环就嫩实现这个效果!
例子3 :在一个多维数组当中添加一个一维数组?
$newlist=Array(
[0] => Array ( [description] => <p dir="ltr">[鼓掌] [addtime] => 2015-1-31 11:27 ) [1] => Array ( [description] => <p dir="ltr">[呵呵] [addtime] => 2015-1-31 11:27 )
)
$users=Array(
[0] => Array ( [nickname] => 时光 [face_url] => http://cshk.oss-cn-hangzhou.aliyuncs.com/user/face/77/head.jpg ) )
for($i=0;$i<count($newlist);$i++){ //把昵称和头像插入到$newlist数组当中去
$newlist[$i]['nickname']=$users['0']['nickname'];
$newlist[$i]['face_url']=$users['0']['face_url'];
}
得到的效果是:
$newlist=Array(
[0] => Array ( [description] => <p dir="ltr">[鼓掌] [addtime] => 2015-1-31 11:27
[nickname] => 时光
[face_url] => http://cshk.oss-cn-hangzhou.aliyuncs.com/user/face/77/head.jpg )
[1] => Array (
[description] => <p dir="ltr">[呵呵]
[addtime] => 2015-1-31 11:27
[nickname] => 时光
[face_url] => http://cshk.oss-cn-hangzhou.aliyuncs.com/user/face/77/head.jpg
)
)
总结:在$newlist数组中添加两个元素'nickname'和'face_url' 并且把$users中的数据合并到$newlist数组当中
例子3:如何将一维数组转换成多维数组
function partition( $list, $p ) { //$list 是数组参数,$p 是分成多维数组中下级数组数量;
$listlen = count( $list ); //算出$list数组的元素的数量
$partlen = floor( $listlen / $p ); //算出啊每个数组,保留几个元素
$partrem = $listlen % $p; //取模。意思就是取余,取$partlen得余数
$partition = array(); //定义一个数组来接收
$mark = 0;
for ($px = 0; $px < $p; $px++) {
$incr = ($px < $partrem) ? $partlen + 1 : $partlen; // ? 是三目运算符 如果$px < $partrem 那么输出的值是$partlen + 1,否则是 $partlen
$partition[$px] = array_slice( $list, $mark, $incr ); //array_slice(array,offset,length,preserve) 是offset从规定取出元素的开始位置。
$mark += $incr; //$mark自增,并赋值给$incr
}
return $partition;
}
$citylist = array( "Black Canyon City", "Chandler", "Flagstaff", "Gilbert", "Glendale", "Globe", "Mesa", "Miami",
"Phoenix", "Peoria", "Prescott", "Scottsdale", "Sun City", "Surprise", "Tempe", "Tucson", "Wickenburg" );
// var_dump($citylist);
print_r( partition( $citylist, 3 ) );
得到的效果:
Array
(
[0] => Array
(
[0] => Black Canyon City
[1] => Chandler
[2] => Flagstaff
[3] => Gilbert
[4] => Glendale
[5] => Globe
)
[1] => Array
(
[0] => Mesa
[1] => Miami
[2] => Phoenix
[3] => Peoria
[4] => Prescott
[5] => Scottsdale
)
[2] => Array
(
[0] => Sun City
[1] => Surprise
[2] => Tempe
[3] => Tucson
[4] => Wickenburg
)
)