2

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.

14
  • 1
    You are missing a / in your last <tr> closing tag. Commented Jun 24, 2019 at 4:46
  • @Joseph_J, Yes its my mistake and fixed it now. But problem is still same. Commented Jun 24, 2019 at 4:50
  • can you share How you define $destination and $depart_date? Commented Jun 24, 2019 at 4:51
  • @M.Hemant, this is how I define those $depart_date = $_POST['depart_date']; and $destination = $_POST['destination']; Commented Jun 24, 2019 at 4:52
  • can you share these two array individual? because you shared $_POST in print_r Commented Jun 24, 2019 at 4:58

3 Answers 3

2

Your code as you have it displayed appears to be working just fine.

I have tested this and it works.

$_POST = 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'

    )

);

$destination  = $_POST['destination'];
if (is_array($destination)) {
    $destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING); //<---This is your problem.  You are adding an extra element to the array.
    //print_r($destination);
}

if (!empty($_POST['depart_date'])) {
    $depart_date = $_POST['depart_date'];
    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!');
          }
        }
    }
}

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

echo $dtble;

UPDATE:

Your problem is here:

In your $destination array that you are trying to sanitize you are adding another element to the array with this line:

$destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING);

I would loop across the array and sanitize each value as you iterate through the array.

If I remove the line of code, your code works just fine.

This returns:

Destination     Depart Date
a               2019-06-04
b               2019-06-06
c               2019-06-13
d               2019-06-22

filter_input is not meant to be used on an array, only a single value. I suggest that you read the docs for filter_input_array and adjust your code to suit.

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

Comments

1

You can use array_combine with foreach

$arr = array_combine($destination, $depart_date);
$html =  "<table>
            <tr>
              <th>Destination</th>
              <th>Depart Date</th>
            </tr>";
foreach ($arr as $key => $value) {
    $html .= "<tr>
            <td>{$key}</td>
            <td>{$value}</td>
        </tr>";
}
$html .= "</table>";
echo $html;

Comments

1

You can use array_combine() to combine them in an associative array. Then you can easily loop over them. Make sure you have depart date for every destination and vice versa.

<?php

   $destination = $_POST['destination'];
   $depart_date = $_POST['depart_date'];
   echo '<table>';
   echo '<tr>';
   echo '<th>Destination</th>';
   echo '<th>Depart Date</th>';
   echo '</tr>';
   foreach(array_combine($destination,$depart_date) as 
   $destination=>$depart_date)
   {
      echo "<tr>";
      echo "<td>$destination</td>";
      echo "<td>$depart_date</td>"
      echo "</tr>";
    }
    echo "</table>";

2 Comments

what is the problem is OP? AND why did you suggesting array_combine() to combine them ?
That way looping over those array will be easier to display in a table, moreover, arrays seems to related to each other, so making an associative array will be good assuming thier count will be same.

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.