循环中的数组数据不正确

时间:2017-03-11 03:06:26

标签: php arrays

我有一系列变量在下一个循环中收集。 我想将它们存储在一个数组中,以便稍后我可以在一些javascript中访问它们,所以我设置了

$remainingTime = array();

之前(!$ listing-> EOF){

填充变量后,我添加了

$remainingTime[] = array( $remainingDay, $remainingHour, $remainingMinutes, $remainingSeconds );

如果我这样做

echo "<pre>";
print_r($remainingTime);
echo "</pre>";

循环结束后,我得到以下输出

Array
(
[0] => Array
    (
        [0] => 6
        [1] => 14
        [2] => 34
        [3] => 1
    )

)
Array
(
[0] => Array
    (
        [0] => 6
        [1] => 14
        [2] => 34
        [3] => 1
    )

[1] => Array
    (
        [0] => 5
        [1] => 15
        [2] => 33
        [3] => 12
    )

)
Array
(
[0] => Array
    (
        [0] => 6
        [1] => 14
        [2] => 34
        [3] => 1
    )

[1] => Array
    (
        [0] => 5
        [1] => 15
        [2] => 33
        [3] => 12
    )

[2] => Array
    (
        [0] => 8
        [1] => 16
        [2] => 4
        [3] => 33
    )

)
Array
(
[0] => Array
    (
        [0] => 6
        [1] => 14
        [2] => 34
        [3] => 1
    )

[1] => Array
    (
        [0] => 5
        [1] => 15
        [2] => 33
        [3] => 12
    )

[2] => Array
    (
        [0] => 8
        [1] => 16
        [2] => 4
        [3] => 33
    )

[3] => Array
    (
        [0] => 2
        [1] => 5
        [2] => 38
        [3] => 17
    )

) 

因此,它似乎每次进行循环时都在构建一个新数组,而不是仅仅添加下一个数据集。

构建数组我在哪里出错了?

1 个答案:

答案 0 :(得分:0)

您使用的赋值语法($ remainingTime [])将您指定的值作为新元素推送到数组中。不使用方括号就试一试。