您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 临沧分类信息网,免费分类信息发布

php怎么去掉数组中的空元素

2024/3/13 16:13:10发布27次查看
php去掉数组中的空元素的方法:1、使用“function myfunction($v) {...}”方法去除空元素;2、使用“foreach ($urlarr as $key => $val) {...}”方法去掉数组中的空元素。
本文操作环境:windows7系统、php7.1版,dell g3电脑
php怎么去掉数组中的空元素?
php数组去除空元素的方法
去除空元素方法一:(保持键值不变)
原数组:
<?phparray( [0] => sdfdsdsdsfrrrrrrr [1] => fdsfdsdsfds [2] => fdsfdsdsfdsds [3] => fdsdsfdsfds [4] => [5] => [6] => [7] => fdsfdsfdsfds [8] => fdsfdsfdsfdsfdsfsd [9] => ssssssss [10] => [11] => sssssssssssssssssssss) ?>
处理之后数组
<?phparray( [0] => sdfdsdsdsfrrrrrrr [1] => fdsfdsdsfds[2] => fdsfdsdsfdsds [3] => fdsdsfdsfds [7] => fdsfdsfdsfds [8] => fdsfdsfdsfdsfdsfsd [9] => ssssssss [11] => sssssssssssssssssssss)?>
使用方法:
<?phpfunction myfunction($v) { if ($v != "") { return true; } return false;}print_r(array_filter($urlarr, "myfunction"));?>
去除空元素方法二:(键值自增)
原数组:
<?phparray([0] => sdfdsdsdsfrrrrrrr[1] => fdsfdsdsfds[2] => fdsfdsdsfdsds[3] => fdsdsfdsfds[4] =>[5] =>[6] =>[7] => fdsfdsfdsfds[8] => fdsfdsfdsfdsfdsfsd[9] => ssssssss[10] =>[11] => sssssssssssssssssssss)
处理之后数组:
array([0] => sdfdsdsdsfrrrrrrr[1] => fdsfdsdsfds[2] => fdsfdsdsfdsds[3] => fdsdsfdsfds[4] => fdsfdsfdsfds[5] => fdsfdsfdsfdsfdsfsd[6] => ssssssss[7] => sssssssssssssssssssss)
使用方法:
$arr = array(); foreach ($urlarr as $key => $val) { if (empty($val)) { continue; } $arr[] = $val; }
推荐学习:《php视频教程》
以上就是php怎么去掉数组中的空元素的详细内容。
临沧分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录