Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查数组索引是否包含值_Php_Arrays - Fatal编程技术网

Php 如何检查数组索引是否包含值

Php 如何检查数组索引是否包含值,php,arrays,Php,Arrays,我有一个数组,在不同的点上有一些值。我想检查索引中是否有值,然后将其放入从0开始的新数组中,然后放入索引1,然后放入索引2的下一个值,依此类推。我需要把它缩短,把它们都移到左边 Array ( [0] => 53 [1] => [2] => 55 [3] => 76 [4] => [5] => [6] => [7] => ) 新阵列将是: newArray ( [0] => 53 [1] =>55 [2] => 76) 也许是

我有一个数组,在不同的点上有一些值。我想检查索引中是否有值,然后将其放入从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
    }
}
$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);
for($i=0;$i
您可以使用

           array_filter($yourArray) 

它将为您删除所有空值

请尝试
array\u filter
,这正好使

var_dump(array_filter(array(0 => 55, 1 => 60, 2 => null)))

要仅获取非
NULL
或空的值,可以使用如下方法:

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
    }
}
$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);

如果要检查索引是否有值,请执行以下操作:

$variable = array ([0] => 53, [1] => , [2] => 55, [3] => 76, [4] => , [5] => , [6] => , [7] => )

foreach ($variable as $key => $value) {
        var_dump($key.' => '.$value);
    }

很简单:
if($array[$i])
,然后将值放入另一个数组中,另一个计数器从0开始

$array = array(76, NULL, NULL, 56);
$count = 0;

for ($i=0; $i < sizeof($array); $i++)
{ 
    if($array[$i])
    {
        $arr[$count] = $array[$i];
        $count++;
    }
};

print_r($array);
print_r($arr);
$array=array(76,NULL,NULL,56);
$count=0;
对于($i=0;$i
您的“可能类似于此”解决方案很好。但我不知道如何在php中实现该解决方案…
如果($questionWorth[$i]!='')$newArray[]=$questionWorth[$i]“但我不知道如何在php中实现该解决方案”-因为你很懒-你想让我们为你做你的工作吗?不,它将删除所有空值及其值keys@DinoBicBoi很高兴我能帮上忙虽然它不是那样工作的,它是([0]=>76,[3]=>56)而不是0和one@DinoBicBoi-现在应该可以了,过滤掉空值后,只需使用
array\u values()