dani33l_87 Posted July 4, 2014 Share Posted July 4, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/289443-convert-form-value-into-array/ Share on other sites More sharing options...
Solution Barand Posted July 4, 2014 Solution Share Posted July 4, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/289443-convert-form-value-into-array/#findComment-1483879 Share on other sites More sharing options...
dani33l_87 Posted July 5, 2014 Author Share Posted July 5, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289443-convert-form-value-into-array/#findComment-1483912 Share on other sites More sharing options...
Ch0cu3r Posted July 5, 2014 Share Posted July 5, 2014 (edited) 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 July 5, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/289443-convert-form-value-into-array/#findComment-1483915 Share on other sites More sharing options...
dani33l_87 Posted July 5, 2014 Author Share Posted July 5, 2014 Yes Ch0cu3 you are right. Now is working how I want. Thanks guys for the help. Quote Link to comment https://forums.phpfreaks.com/topic/289443-convert-form-value-into-array/#findComment-1483917 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.