为什么数组有一个额外的"[index] =>“;最后,和array_combine出错


Why does the array have an extra "[index] => " in the end, and array_combine error

我在两个数组上使用array_combine()函数,并使用以下代码:

if (($line = fgetcsv($file)) !== FALSE) {
    //$line is an array of CSV elements
    print_r($line);
    print_r($slots_for_quiz);
    $combined_array = array_combine($slots_for_quiz, $line);
    print_r($combined_array); //check 
} else {echo 'FALSE';}

输出为:

Array ( [0] => Alpha [1] => Beta [2] => Gamma [3] => )  Array ( [0] => 1 [1] => 2 [2] => 3 ) 
Warning: array_combine(): Both parameters should have an equal number of elements

我们可以看到 $line 是:数组([0]=>βα[1]=>[2]=>γ [3] => )

$slots_for_quiz 是:数组([0]=> 1 [1]=> 2 [2]=> 3)

我的问题是1. 为什么我在$line的末尾得到[3] =>(这甚至不是一个关联数组)。2. 关于警告:为什么它说我的数组没有相同数量的元素。它们都有3元素,不是吗?

也许你可以这样做:

$line = array_filter($line);
$combined_array = array_combine($slots_for_quiz, $line);

应该删除这个空值,并且现在可能正确地对齐。

或者你可以直接打开。

array_pop($line);
$combined_array = array_combine($slots_for_quiz, $line);