您能告诉我从数组中删除项目的方法吗?你觉得这样好吗?
那要看:
$a1 = array('a' => 1, 'b' => 2, 'c' => 3);
unset($a1['b']);
// array('a' => 1, 'c' => 3)
$a2 = array(1, 2, 3);
unset($a2[1]);
// array(0 => 1, 2 => 3)
// note the missing index 1
// solution 1 for numeric arrays
$a3 = array(1, 2, 3);
array_splice($a3, 1, 1);
// array(0 => 1, 1 => 3)
// index is now continous
// solution 2 for numeric arrays
$a4 = array(1, 2, 3);
unset($a4[1]);
$a4 = array_values($a4);
// array(0 => 1, 1 => 3)
// index is now continous
一般来说unset() http://de3.php.net/manual/en/function.unset.php对于哈希表(字符串索引数组)来说是安全的,但是如果您必须依赖连续数字索引,则必须使用array_splice() http://de3.php.net/manual/en/function.array-splice.php或组合unset()
and array_values() http://de3.php.net/manual/en/function.array-values.php.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)