我有一个数组,在不同位置有一些值。我想检查索引中是否有一个值,然后将其放入从0开始的新数组中,然后放入索引1,然后在索引2中放入下一个值,依此类推。我需要缩短它,然后将它们全部移到左侧。
Array ( [0] => 53 [1] => [2] => 55 [3] => 76 [4] => [5] => [6] => [7] => )
新数组将是:
newArray ( [0] => 53 [1] =>55 [2] => 76)
也许是这样的:
for ($i=0; $i < sizeof($questionWorth); $i++)
{
if($questionWorth[$i] has a value)
{
put it in new array starting at index zero
then increment the index of new array
}
}
要仅获取非NULL
或为空的值,可以使用array_filter()
,array_values()
如下所示:
$array = array(76, NULL, NULL, 56);
// remove empty values from array, notice that since no callback
// is supplied values that evaluates to false will be removed
$array = array_filter($array);
// since array_filter will preserve the array keys
// you can use array_values() to reindex the array numerically
$array = array_values($array);
// prints Array ( [0] => 76 [1] => 56 )
print_r($array);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句