脚本宝典收集整理的这篇文章主要介绍了如何在php中使用array_splice删除单个数组成员?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
Array ( [0] => Array ( [TyPEFlag] => S [qty] => 1 [denom] => 25 [certMessage] => [totalPrice] => 25 ) [1] => Array ( [TypeFlag] => C [qty] => 2 [denom] => 25 [certMessage] => [totalPrice] => 50 ) [2] => Array ( [TypeFlag] => V [qty] => 2 [denom] => 25 [certMessage] => test [totalPrice] => 50 ) )
我想完全删除第二个元素(索引为1的数组; TypeFlag = C等)我不想用任何东西替换它;只是为了返回剩下的两个元素的数组.我试过这个(其中cart是数组名称):
$cart = array_splice($cart,1,1);
但是当我做一个print_r时,我最终得到的是:
Array ( [0] => Array ( [TypeFlag] => C [qty] => 2 [denom] => 25 [certMessage] => [totalPrice] => 50 ) )
你在做:
$cart = array_splice($cart,1);
因此,您要提取第二个元素(索引1)并将结果分配回$cart,覆盖原始数组.
要完全删除第二个元素,请不要将array_splice的返回值分配回$cart.所以这样做:
array_splice($cart,1);
这是有效的,因为数组是通过引用函数传递的.
此外,如果您想从数组中删除单个元素,则使用unset更高效,更清晰:
unset($cart[1]);
以上是脚本宝典为你收集整理的如何在php中使用array_splice删除单个数组成员?全部内容,希望文章能够帮你解决如何在php中使用array_splice删除单个数组成员?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。