数组项删除 Thinkphp
请问大神,怎么根据下面取出的字符串,删除数组中含有这字符串的一项呢比如我单击button后,就能删除goods_id为M210150518040351的哪一项。我知道删除...
请问大神,怎么根据下面取出的字符串,删除数组中含有这字符串的一项呢
比如我单击button后,就能删除goods_id 为M210150518040351的哪一项。我知道删除项用array_splice()这函数,可offset怎么确定呢?
我用foreach能找到和它相同的这一项,可if里面怎么处理呢 展开
比如我单击button后,就能删除goods_id 为M210150518040351的哪一项。我知道删除项用array_splice()这函数,可offset怎么确定呢?
我用foreach能找到和它相同的这一项,可if里面怎么处理呢 展开
2个回答
展开全部
<?php
$data=array(
0=>array(
'goods_id' => 'aaa1',
'goods_num' => 1,
'goods_title' => 't1',
'goods_addtime' => 111224551554,
),
1=>array(
'goods_id' => 'aaa2',
'goods_num' => 2,
'goods_title' => 't2',
'goods_addtime' => 111224551554,
),
2=>array(
'goods_id' => 'aaa3',
'goods_num' => 4,
'goods_title' => 't3',
'goods_addtime' => 111224551554,
)
);
$del_goodsid='aaa1'; #待删除的goods_id值
$newdata=array();
foreach($data as $v){
if($v['goods_id']!=$del_goodsid){
$newdata[]=$v;
}
}
#删除aaal后的数组
var_dump($newdata);
?>
$data=array(
0=>array(
'goods_id' => 'aaa1',
'goods_num' => 1,
'goods_title' => 't1',
'goods_addtime' => 111224551554,
),
1=>array(
'goods_id' => 'aaa2',
'goods_num' => 2,
'goods_title' => 't2',
'goods_addtime' => 111224551554,
),
2=>array(
'goods_id' => 'aaa3',
'goods_num' => 4,
'goods_title' => 't3',
'goods_addtime' => 111224551554,
)
);
$del_goodsid='aaa1'; #待删除的goods_id值
$newdata=array();
foreach($data as $v){
if($v['goods_id']!=$del_goodsid){
$newdata[]=$v;
}
}
#删除aaal后的数组
var_dump($newdata);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询