PHP 删除数组中某一个值元素
211 views
0
PHP 删除数组中某一个值元素,有多种方式可以实现
一、for 循环删除值元素
// for 循环删除值元素 function remove_array_item_by_for() { // key 为字符 $mimvp_array = array( "blog"=>"blog.mimvp.com", "site"=>"site.mimvp.com", "shop"=>"shop.mimvp.com", "proxy"=>"proxy.mimvp.com", "money"=>"money.mimvp.com", "domain"=>"domain.mimvp.com"); var_dump($mimvp_array); $delete_item = "proxy.mimvp.com"; foreach($mimvp_array as $key=>$value) { if($value == $delete_item) { // 判断 value unset($mimvp_array[$key]); // 删除 key } } var_dump($mimvp_array); // key 为数字下标 $mimvp_array = array( "0"=>"blog.mimvp.com", "1"=>"site.mimvp.com", "2"=>"shop.mimvp.com", "3"=>"proxy.mimvp.com", "4"=>"money.mimvp.com", "5"=>"domain.mimvp.com"); var_dump($mimvp_array); $delete_item = "proxy.mimvp.com"; foreach($mimvp_array as $key=>$value) { if($value == $delete_item) { // 判断 value unset($mimvp_array[$key]); // 删除 key } } var_dump($mimvp_array); // key 数字下标删除丢失 $mimvp_array = array_values($mimvp_array); // 重建 key 数字下标方式1 var_dump($mimvp_array); $mimvp_array = array_merge($mimvp_array); // 重建 key 数字下标方式2 var_dump($mimvp_array); }
运行结果:
/Users/homer/Workspace/miMvp_demo/tmp_php.php:13: array (size=6) 'blog' => string 'blog.mimvp.com' (length=14) 'site' => string 'site.mimvp.com' (length=14) 'shop' => string 'shop.mimvp.com' (length=14) 'proxy' => string 'proxy.mimvp.com' (length=15) 'money' => string 'money.mimvp.com' (length=15) 'domain' => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:20: array (size=5) 'blog' => string 'blog.mimvp.com' (length=14) 'site' => string 'site.mimvp.com' (length=14) 'shop' => string 'shop.mimvp.com' (length=14) 'money' => string 'money.mimvp.com' (length=15) 'domain' => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:30: array (size=6) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'proxy.mimvp.com' (length=15) 4 => string 'money.mimvp.com' (length=15) 5 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:37: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 4 => string 'money.mimvp.com' (length=15) 5 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:40: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'money.mimvp.com' (length=15) 4 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:43: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'money.mimvp.com' (length=15) 4 => string 'domain.mimvp.com' (length=16)
二、php 自带函数删除值元素
// php 自带函数删除值元素 function remove_array_item_by_func() { // key 为字符 $mimvp_array = array( "blog"=>"blog.mimvp.com", "site"=>"site.mimvp.com", "shop"=>"shop.mimvp.com", "proxy"=>"proxy.mimvp.com", "money"=>"money.mimvp.com", "domain"=>"domain.mimvp.com"); var_dump($mimvp_array); $delete_item = "proxy.mimvp.com"; $mimvp_array = array_merge(array_diff($mimvp_array, array($delete_item))); // 计算出差异数组,并重建key下标索引 var_dump($mimvp_array); // key 为数字下标 $mimvp_array = array( "0"=>"blog.mimvp.com", "1"=>"site.mimvp.com", "2"=>"shop.mimvp.com", "3"=>"proxy.mimvp.com", "4"=>"money.mimvp.com", "5"=>"domain.mimvp.com"); var_dump($mimvp_array); $delete_item = "proxy.mimvp.com"; $key = array_search($delete_item, $mimvp_array); // 先找出删除值元素的key array_splice($mimvp_array, $key, 1); // 自动实现重置序列值(只对数字下标有效,字母下标报错) var_dump($mimvp_array); // key 数字下标删除丢失 var_dump($mimvp_array); $delete_item = "proxy.mimvp.com"; $mimvp_array = array_merge(array_diff($mimvp_array, array($delete_item))); // 计算出差异数组,并重建key下标索引 var_dump($mimvp_array); var_dump($mimvp_array); $delete_array = array("blog.mimvp.com", "proxy.mimvp.com"); $mimvp_array = array_merge(array_diff($mimvp_array, $delete_array)); // 计算出差异数组,并重建key下标索引 var_dump($mimvp_array); }
运行结果:
/Users/homer/Workspace/miMvp_demo/tmp_php.php:57: array (size=6) 'blog' => string 'blog.mimvp.com' (length=14) 'site' => string 'site.mimvp.com' (length=14) 'shop' => string 'shop.mimvp.com' (length=14) 'proxy' => string 'proxy.mimvp.com' (length=15) 'money' => string 'money.mimvp.com' (length=15) 'domain' => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:60: array (size=5) 'blog' => string 'blog.mimvp.com' (length=14) 'site' => string 'site.mimvp.com' (length=14) 'shop' => string 'shop.mimvp.com' (length=14) 'money' => string 'money.mimvp.com' (length=15) 'domain' => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:70: array (size=6) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'proxy.mimvp.com' (length=15) 4 => string 'money.mimvp.com' (length=15) 5 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:74: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'money.mimvp.com' (length=15) 4 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:76: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'money.mimvp.com' (length=15) 4 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:79: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'money.mimvp.com' (length=15) 4 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:81: array (size=5) 0 => string 'blog.mimvp.com' (length=14) 1 => string 'site.mimvp.com' (length=14) 2 => string 'shop.mimvp.com' (length=14) 3 => string 'money.mimvp.com' (length=15) 4 => string 'domain.mimvp.com' (length=16) /Users/homer/Workspace/miMvp_demo/tmp_php.php:84: array (size=4) 0 => string 'site.mimvp.com' (length=14) 1 => string 'shop.mimvp.com' (length=14) 2 => string 'money.mimvp.com' (length=15) 3 => string 'domain.mimvp.com' (length=16)
上面的例子中$delete_item是一个值,如果$delete_item是一个数组$delete_array或者其他复杂数据结构,从$mimvp_array中删除所有$delete_array包含的元素,上面的方法同样有效
参考推荐:
PHP 输出字符串(echo,print,printf,sprinf,print_r,var_dump)
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2019-03-27 05:49:01
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: PHP 删除数组中某一个值元素 (米扑博客)