- $arr = array('a','b','c','d');
- unset($arr[1]);
- print_r($arr);
- ?>
结果:
Array ( [0] => a [2] => c [3] => d )
如何做到缺少的元素会被填补并且数组会被重新索引?
答案是array_splice():
例子:
- $arr = array('a','b','c','d');
- array_splice($arr,1,1);
- print_r($arr);
- ?>
-
结果:
Array ( [0] => a [1] => c [2] => d )
删除数组中特定元素(bbs.it-home.org 脚本学堂):
- $arr2 = array(1,3, 5,7,8);
- foreach ($arr2 as $key=>$value)
- {
- if ($value === 3)
- unset($arr2[$key]);
- }
- var_dump($arr2);
- ?>
删除空数组:
- $array = ('a' => "abc", 'b' => "bcd",'c' =>"cde",'d' =>"def",'e'=>"");
- array_filter($array);
- echo "
"; - print_r($array);
- ?>
结果:
Array (
[a] => abc
=> bcd
[c] => cde
[d] => def
)
总结:
array_splice()函数删除的话,数组的索引值也变化了。
unset()函数删除的话,数组的索引值没有变化。 |