Jump to content

Convert Form Value Into Array


Go to solution Solved by Barand,

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

  • Solution

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'
Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.