Skip to content
Advertisement

How to convert an output array to a string or variable using PHP?

I am getting an output in array:

    array (size=3)
  8 => string 'Mexico' (length=6)
  24 => string 'UAE' (length=3)
  34 => string 'India' (length=5)

    array (size=9)
  2 => string '' (length=0)
  8 => string 'Large' (length=5)
  14 => string '' (length=0)
  19 => string '' (length=0)
  23 => string '' (length=0)
  24 => string 'Micro' (length=5)
  34 => string 'Large' (length=5)
  35 => string '' (length=0)
  38 => string '' (length=0)

I want the output in the following format: Which means need to compare the id of both.

Mexico - Large (8)
UAE - Micro (24)
India - Large (34)

PHP SRIPT

<?php
  $entities = $check_list = [];

  foreach($_POST as $k => $v) {
    if(preg_match("/^check_list(d+)$/", $k, $matches))
      $check_list[intval($matches[0])] = $v;
    unset($matches);
    if(preg_match("/^entity_selected(d+)$/", $k, $matches))
      $entities[intval($matches[0])] = $v;
  };
            var_dump($_POST['check_list']);

            var_dump($_POST['entity_selected']);

            echo implode(" ",$_POST['entity_selected'])."<br>";

            echo implode(" ",$_POST['check_list'])."<br>";
?>

I need to compare the variable and insert the data accordingly to the DB. But i am not able to make this data into variables. How can I do that?

This is my PHP Form script:

<form method="POST" action="nextpage1.php">
  <table border="1">
    <?php
      $i = 0;
      while($row1 = mysqli_fetch_array($result_country))  {
        $country1 = $row1['country'];
        $entity1 = $row1['entity'];
        echo "<tr>";
        ?>
          <td>
            <input name="check_list[<?php echo $i; ?>]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
          </td>
          <td>
            <?php
              if($entity1=="Yes") {
                ?>
                  <select class="form-control selectpicker" name="entity_selected[<?php echo $i; ?>]">
                                      <option value="">Select</option>

                    <option value="Micro">Micro</option>
                    <option value="Small">Small</option>
                    <option value="Large">Large</option>
                  </select>
                <?php
              };
            ?>
          </td>
        <?php
        $i++;
        echo "</tr>";
      };
    ?>
  </table>

enter image description here

Basically the form script consists of some countries with checkboxes and a drop down to select the entities. So which ever check box is selected that value (country name) and the entity has to be captured and inserted to the DB.

Advertisement

Answer

You are over complicating it. Just loop through $_POST['check_list'] and use its key to find the associated value in $_POST['entity_selected']:

foreach ($_POST['check_list'] as $key => $country) {
    echo $country . ' - ' . $_POST['entity_selected'][$key] . ' (' . $key . ')'. "n";
}

This assumes there is always a corresponding key in $_POST['entity_selected'] as in $_POST['check_list'].

If you want to make the output a little bit easier on the eyes you can also use printf() to help with the formatting:

foreach ($_POST['check_list'] as $key => $country) {
    printf('%s - $s (%u)%s', $country, $_POST['entity_selected'][$key], $key, "n");
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement