Jump to content

Convert Form Value Into Array


dani33l_87

Recommended Posts

I have this form:

 

 

<form method="get">
 
Distance: <select name="miles">
<option value="25" selected="selected">25 Miles</option>
<option value="50">50 Miles</option>
<option value="100">100 Miles</option>
</select>
 
<p><input type="submit" value="Submit">
 
</form>
 
 
 
and the php script file:
 
<?php 
 
$miles=$_GET['miles'];      // this is how I get the value of the input miles and try to convert the value (25) into a array
 
$25=array("close", "closest", "hot")  // the value 25 will have three shown variants
 
 
 
echo "This is, $25[2]";   // result  This is hot.   - this is the result that I want
echo "This is, $25[1]";   // closest 
echo "This is, $25[0]";  // close
 
?>

 

I have problems to split a value into array, because for each value (25, 50, 100...) will be shown different echo words like in the example. I look on the internet but I can t really find something to help me with this. 

 

It is a solution in PHP that will don t need to change the html form?

 

Link to comment
https://forums.phpfreaks.com/topic/289443-convert-form-value-into-array/
Share on other sites

I would set up an array of the possible values like this


    $values = array (
                25 => array ('close', 'closest', 'hot'),
                50 => array ('aaa', 'bbb', 'ccc'),
               100 => array ('xxx', 'yyy', 'zzz') 
            );
            

then

    echo $values[25][2];     //--> hot
    echo $values[100][1];    //--> yyy

The structure for the array look nice, but what about the connection between the form and the array.

 

Because now is not taking into the consideration the value that come through input:  $miles=$_GET['miles'];   

 

    $miles = array (
                25 => array ('close', 'closest', 'hot'),
                50 => array ('aaa', 'bbb', 'ccc'),
               100 => array ('xxx', 'yyy', 'zzz') 
            );
 
In this case we have all the three values but only one need to be shown.  For example:
 
echo $miles[2]   will show ccc    // when 50 is the form outcome 
 
if the outcome is 100 then 
 
echo $miles[2] will show zzz
 
if will be 25
 
echo $miles[2] will show hot.
 

I would like to keep &miles[array number] because I don`t know the output and will make easier to be used in a code.

 

This is pretty much the result wanted.

Barands solution is correct, I think you have missunderstod how to implement it into your code.

 

Example usage:

$miles = $_GET['miles']; // get the miles

// $miles is used as the array key for the $values array.
$result = $values[ $miles ][2]; // returns the third item corresponding to the $miles key. Example if $miles is 25, then $result will be 'hot'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.