本文介绍了什么是内置的PHP函数与QUOT; COM pressing"或QUOT;整理"数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这会是微不足道的code自己,但在没有更多code来维持,如果它内置于PHP已经的利益,有一个内置的功能COM pressing一个PHP数组?换句话说,假设所以我创建磁盘阵列:
I know it'd be trivial to code myself, but in the interest of not having more code to maintain if it's built in to PHP already, is there a built-in function for "compressing" a PHP array? In other words, let's say I create an array thus:
$array = array();
$array[2000] = 5;
$array[3000] = 7;
$array[3500] = 9;
我要的是一个数组,其中$数组[0] == 5,$阵列[1] == 7,$阵列[2] == 9。
What I want is an array where $array[0] == 5, $array[1] == 7, $array[2] == 9.
我可以这样做:
function array_defragment($array) {
$squashed_array = array();
foreach ($array as $item) {
$squashed_array[] = $item;
}
return $squashed_array;
}
...但它似乎喜欢那种事,将被内置到PHP - 我不能在这个文档中找到
...but it seems like the kind of thing that would be built in to PHP - I just can't find it in the docs.
推荐答案
只需使用:
$array = array();
$array[2000] = 5;
$array[3000] = 7;
$array[3500] = 9;
$array = array_values($array);
var_dump($array === array(5, 7, 9));
这篇关于什么是内置的PHP函数与QUOT; COM pressing"或QUOT;整理"数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!