PHP创建多维数组

时间:2015-05-08 02:45:55

标签: php multidimensional-array

我正在尝试使用foreach循环在PHP中创建一个多维数组。这是迄今为止的代码:

for($day1=$day; $day1<=2; $day1++)
{
$train_list['option'][] = array('day' => $day1);
$train = $train_xpath->query('//li/div/div[contains(@class, "smallFont farelist")]');
if($train->length > 0)
{
    foreach($train as $f) 
    {
        $time_s =  $train_xpath->evaluate('string(./div[@class="train-time"]/text()[1])', $f);
        $time_e =  $train_xpath->evaluate('string(./div[@class="train-time"]/text()[2])', $f);
        $fare = $train_xpath->evaluate('string(./div[@class="train-info"]/div/div[@class="total-price"])', $f);
        $train_list['option'][0]["value"][]=  array('train_no' => $train_xpath->evaluate('string(./div[@class="train-no"])', $f),
                                                     'departure_time' => "$time_s ",
                                                     'arrival_time' => "$time_e ",
                                                     'price' => $fare );

                    }
}
}

此代码的输出如下所示:

    Array
(
    [option] => Array
        (
            [0] => Array
                (
                    [day] => 1
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [train_no] => D7  372
                                    [departure_time] => 10:00 
                                    [arrival_time] => 14:40 
                                    [price] =>  69.00
                                )

                            [1] => Array
                                (
                                    [train_no] => D7  376
                                    [departure_time] => 17:45 
                                    [arrival_time] => 22:25 
                                    [price] =>  59.00
                                )

                        )

                )

            [1] => Array
                (
                    [day] => 2
                )

        )

)

但是,这不是意图。所需的数组应如下所示:

Array
(
    [option] => Array
        (
            [0] => Array
                (
                    [day] => 1
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [train_no] => D7  372
                                    [departure_time] => 10:00 
                                    [arrival_time] => 14:40 
                                    [price] =>  59.00
                                )

                            [1] => Array
                                (
                                    [train_no] => D7  376
                                    [departure_time] => 17:45 
                                    [arrival_time] => 22:25 
                                    [price] =>  49.00
                                )

                        )

                )

            [1] => Array
                (
                    [day] => 2
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [train_no] => D7  372
                                    [departure_time] => 10:00 
                                    [arrival_time] => 14:40 
                                    [price] =>  59.00
                                )

                            [1] => Array
                                (
                                    [train_no] => D7  376
                                    [departure_time] => 17:45 
                                    [arrival_time] => 22:25 
                                    [price] =>  69.00
                                )

                        )

                )

        )

)

如何修改代码以实现目标?

1 个答案:

答案 0 :(得分:1)

您正在覆盖索引0.为了不这样做并使用未知数量的条目填充数组,您需要首先构建条目,然后分配给数组的下一个值。如果您知道条目数,则可以使用计数器并按该索引访问该数组。

试试这个:

go to <ADDRESS>Bolivar Venezuela 5 </ADDRESS>

希望它有所帮助!