1

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?

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.