This is how i define my array from $_POST
$destination = $_POST['destination'];
$depart_date = $_POST['depart_date'];
print_r result as below:
Array
(
[destination] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[depart_date] => Array
(
[0] => 2019-06-04
[1] => 2019-06-06
[2] => 2019-06-13
[3] => 2019-06-22
)
)
Now I want to echo these arrays into a HTML table.
This is how I tried it:
if (is_array($destination)) {
$dtble = "<table>
<tr>
<th>Destination</th>
<th>Depart Date</th>
</tr>";
foreach($destination as $k => $v){
$d = $depart_date[$k];
$dtble .= "<tr>
<td>{$v}</td>
<td>{$d}</td>
<tr>";
}
$dtble .= "</table>";
}
But its give an output something like this:
Destination Depart Date
a 2019-06-04
b 2019-06-06
c 2019-06-13
d 2019-06-22
2019-06-04
** NEW UPDATES** This is how I defined those two arrays and its output:
$destination = array_values(array_unique($_POST['destination']));
if (is_array($destination)) {
$destination[] = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING);
}
echo '<pre>',print_r($destination).'</pre>';
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] =>
)
1
// Depart Date
if (!empty($_POST['depart_date'])) {
$depart_date = array_values(array_unique($_POST['depart_date']));// to remove duplicate and re-index array
if (is_array($depart_date)) {
foreach($depart_date as $dt){
if (preg_match("/\d{4}\-\d{2}-\d{2}/", $dt)) {
$depart_date[] = $dt;
} else{
array_push($errors, '- Depart date is NOT in required format!');
}
}
}
}
echo '<pre>',print_r($depart_date).'</pre>';
Array
(
[0] => 2019-06-03
[1] => 2019-06-04
[2] => 2019-06-05
[3] => 2019-06-06
[4] => 2019-06-03
[5] => 2019-06-04
[6] => 2019-06-05
[7] => 2019-06-06
)
You can see date 2019-06-04 is duplicating.
Can anybody tell how to figure this out.
/in your last<tr>closing tag.$destinationand$depart_date?$depart_date = $_POST['depart_date'];and$destination = $_POST['destination'];$_POSTin print_r