php的数组问题,一个二维数组怎么取出里面的部分元素组成一个新数组?
比如这个二维数组:$arr=Array([0]=>Array([id]=>2[nickname]=>chenjudong[email]=>)[1]=>Array([id]...
比如这个二维数组:
$arr=Array
(
[0] => Array
(
[id] => 2
[nickname] => chenjudong
[email] =>
)
[1] => Array
(
[id] => 8
[nickname] => 天使爱美丽
[email] =>
)
[2] => Array
(
[id] => 15
[nickname] => 倩,我是宁采臣
[email] =>
)
[3] => Array
(
[id] => 1
[nickname] => ruiwurensheng
[email] =>
)
[4] => Array
(
[id] => 11
[nickname] => 我爱爱坏心情
[email] =>
)
)
怎么变成:
$arr=Array
(
[0] => Array
(
[id] => 2
[nickname] => chenjudong
)
[1] => Array
(
[id] => 8
[nickname] => 天使爱美丽
)
[2] => Array
(
[id] => 15
[nickname] => 倩,我是宁采臣
)
[3] => Array
(
[id] => 1
[nickname] => ruiwurensheng
)
[4] => Array
(
[id] => 11
[nickname] => 我爱爱坏心情
)
) 展开
$arr=Array
(
[0] => Array
(
[id] => 2
[nickname] => chenjudong
[email] =>
)
[1] => Array
(
[id] => 8
[nickname] => 天使爱美丽
[email] =>
)
[2] => Array
(
[id] => 15
[nickname] => 倩,我是宁采臣
[email] =>
)
[3] => Array
(
[id] => 1
[nickname] => ruiwurensheng
[email] =>
)
[4] => Array
(
[id] => 11
[nickname] => 我爱爱坏心情
[email] =>
)
)
怎么变成:
$arr=Array
(
[0] => Array
(
[id] => 2
[nickname] => chenjudong
)
[1] => Array
(
[id] => 8
[nickname] => 天使爱美丽
)
[2] => Array
(
[id] => 15
[nickname] => 倩,我是宁采臣
)
[3] => Array
(
[id] => 1
[nickname] => ruiwurensheng
)
[4] => Array
(
[id] => 11
[nickname] => 我爱爱坏心情
)
) 展开
展开全部
$arr=array(array('id'=>2,'nickname'=>'chenjudong','email'=>''), array ('id'=>8,'nickname'=>'天使爱美丽','email'=>''));
$arrNew=array();
foreach($arr as $k=>$v){
$arrNew[$k]=array('id'=>$v['id'],'nickname'=>$v['nickname']);
}
print_r($arrNew);
//重新循环下读取id、nickname组成新数组
展开全部
foreach ($arr as &$value) {
foreach($value as $k=>$v){
if($v == ''){ //假定'email'=>'',或者自己换成合用的,如if($k == 'email')
unset($value[$k]);
}
}
}
追问
谢谢,但是这里的email只是暂时为空呢,email都是有数据的,相当于去掉email这个元素,要怎么去呢?
追答
//只是email的话,下面这个就够了
foreach ($arr as &$value) {
if(isset($value['email'])){
unset($value['email']);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询