phparraysmultidimensional-arrayarray-key

Returning multidimensional array key from selected option in PHP


I'm roughly new in PHP and I'm trying to display the contents of a multidimensional array in a table that was inside a select option list which has been selected by the user. What I'm trying to do is that the array key of the selected option is returned into a new variable such as $keyref = key($myarray); so that I can use the key to display inside the table but i don't know how. Do I use a javascript function such as onChange=function() in the <select> list?

Here is the code:

<?php
$tshirt = array (
  array("T-shirt A",15,"XS"),
  array("T-shirt B",20,"S"),
  array("T-shirt C",25,"M")
  ); //array("t-shirt type", price, "size")

 $keyRef = 0; //the variable that will contain the selected array key
 echo "<select id='shirtlist'>";
 foreach($tshirt as $value){
   echo "<option value='$value)'>$value[0]</option>";
  }
  echo "</select>";

echo "<p>Details of T-shirt:</p>";
  echo "<table>";
  echo "<tr>";
  echo "<th>T-shirt Type</th>";
  echo "<th>Price</th>";
  echo "<th>Size</th>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>".$tshirt[$keyRef][0]."</td>";
  echo "<td>".$tshirt[$keyRef][1]."</td>";
  echo "<td>".$tshirt[$keyRef][2]."</td>";
  echo "</tr>";
  echo "</table>";
?>


The table manage to display the data of the first array but nothing happens when I selected the other options. As for trying javascript function I tried the json_encode($myarray) method but that only returned the Array as String notice.


Solution

  • First of all, you have to understand that PHP is only executed server-side, which means that there is no interaction between the PHP code and the action you are achieving in the browser.

    The only way to return to PHP is to send a new request.

    Depending on what you want to do and which workflow you want in the browser you should choose between pure PHP (each action has to be achieved with client-server request) or using javascript (directly executed in the browser).

    In pure PHP, your feature can be achieved with the following code :

    <?php
    $tshirt = array (
      array("T-shirt A",15,"XS"),
      array("T-shirt B",20,"S"),
      array("T-shirt C",25,"M")
      ); //array("t-shirt type", price, "size")
    
     $keyRef = 0; //the variable that will contain the selected array key
     if(isset($_GET["shortlist"]) && isset($tshirt[$_GET["shirtlist"]])) {
      $keyRef = $_GET["shirtlist"];
     }
    
     echo "<form method='GET'>";
     echo "<select name='shirtlist'>";
     foreach($tshirt as $key => $value){
       $selected = '';
         if ($key == $keyRef) {
             $selected = 'selected';
         }
       echo "<option value='$key' $selected>".$value[0]."</option>";
      }
      echo "</select>";
      echo "<input type='submit' value='submit'/>";
      echo "</form>";
    
    echo "<p>Details of T-shirt:</p>";
      echo "<table>";
      echo "<tr>";
      echo "<th>T-shirt Type</th>";
      echo "<th>Price</th>";
      echo "<th>Size</th>";
      echo "</tr>";
      echo "<tr>";
      echo "<td>".$tshirt[$keyRef][0]."</td>";
      echo "<td>".$tshirt[$keyRef][1]."</td>";
      echo "<td>".$tshirt[$keyRef][2]."</td>";
      echo "</tr>";
      echo "</table>";
    ?>
    

    Note that I use the global $_GET to get the information from the URL query strings. This information is transmitted to PHP when the submit button of the form is clicked, and then the value of the select is accessible in it using the name attribute of the tag (<select name="shirtlist"> means the value is accessible in $_GET["shirtlist"] when the method of the form is GET).

    This code requires an action from the user to update the page with the selection.

    I also added a selected attribute to the select in order to select the displayed article (When the iteration index is the same as the requested shirtlist value).

    The double check if(isset($_GET["shortlist"]) && isset($tshirt[$_GET["shirtlist"]])) { is here to be sure the selected item is in the list, as the URL is accessible to anyone it's possible to change manually the value, it's more secure to change the form method to POST and then access the value using the $_POST global array instead of the $_GET one.