How to manipulate multidimensional arrays with forms

Hi,

I have recently started learning php. I have created a form using HTML. On this form a user has to choose a “Departure City” and after they validate the form, All the possible destinations (stored in a multidimensional table) should show only from the “Departure City selected”.

With my code, all destinations show even those not from the selected city.

How do I show only the Destination cities from the Departure cities selected using a simple code?

$trips= array (

0=> array ('Departure'=>'Paris', 'Arrival'=>'Nantes', 'departureTime'=>'11:00', 'arrivalTime'=>'12:34', 'Driver'=>'Thomas'),

1=> array ('Departure'=>'Orleans', 'Arrival'=>'Nantes', 'departureTime'=>'05:15', 'arrivalTime'=>'09:33', 'Driver'=>'Mathieu'),

2=> array ('Departure'=>'Dublin', 'Arrival'=>'Tours', 'departureTime'=>'07:23', 'arrivalTime'=>'08:50', 'Driver'=>'Nathanael'),

3=> array ('Departure'=>'Paris', 'Arrival'=>'Orleans', 'departureTime'=>'03:00', 'arrivalTime'=>'05:26', 'Driver'=>'Clément'),

4=> array ('Departure'=>'Paris', 'Arrival'=>'Nice', 'departureTime'=>'10:00', 'arrivalTime'=>'12:09', 'Driver'=>'Audrey'),

5=> array ('Departure'=>'Nice', 'Arrival'=>'Nantes', 'departureTime'=>'10:40', 'arrivalTime'=>'13:00', 'Driver'=>'Pollux'),

6=> array ('Departure'=>'Nice', 'Arrival'=>'Tours', 'departureTime'=>'11:00', 'arrivalTime'=>'16:10', 'Driver'=>'Edouard'),

7=> array ('Departure'=>'Tours', 'Arrival'=>'Amboise', 'departureTime'=>'16:00', 'arrivalTime'=>'18:40', 'Driver'=>'Priscillia'),

8=> array ('Departure'=>'Nice', 'Arrival'=>'Nantes', 'departureTime'=>'12:00', 'arrivalTime'=>'16:00', 'Driver'=>'Charlotte'),

);

This is my table list of options. So if the user chooses Paris as the departure city, when he/she submits the form, then the site should show details of arrays 0, 3 and 4 only. If the user chooses Nice as the departure and then submits the form, then the site should show details of arrays 5, 6 and 8 only etc…

Thank you for your help.

foreach ($trips as $k => $v) {
    if ($v['Departure'] == $_POST['DepartureCity']) {
        echo "Depart: {$v['Departure']} Arrive: {$v['Arrival']} Depart Time: {$v['departureTime']} Arrival: {$v['arrivalTime']} Driver:{$v['Driver']}<br>";
    }
}

Thank you so much benanamen!! The code works perfectly!!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.