phparraysmultidimensional-arraygroupinghtml-parsing

Populate a grouped multidimensional array from parsed HTML content


I am trying to create a multidimensional array in PHP using a foreach loop. Here is the code thus far:

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

The output from this code look like this:

    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
                )

        )

)

However, that is not the intent. The desired array should look like this:

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
                                )

                        )

                )

        )

)

How should the code be modified to achieve the goal?


Solution

  • You are overriding index 0. In order not to do so and fill the array with an unknown number of entries, you need to first build the entry and then assign in to the next value of the array. If you know the number of entries, you may use a counter and access the array by that index.

    Try this:

    for($day1=$day; $day1<=2; $day1++)
    {
        $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);
                $option["value"][]=  array('flight_no' => $train_xpath->evaluate('string(./div[@class="train-no"])', $f),
                                                             'departure_time' => "$time_s ",
                                                             'arrival_time' => "$time_e ",
                                                             'price' => $fare );
    
                            }
        }
        $train_list['option'][] = $option;
    }
    

    Wish it helps!