Jimmy Jimmy -4 years ago 104
PHP Question

Output multidimensional array from radio input

I'm trying to write some PHP so that, depending on what radio option was selected, I was to set two variables as my array values. This is how far I got:

<form action="" method="post">
<input type="radio" name="radio" value="volvo">Volvo
<input type="radio" name="radio" value="bmw">BMW
<input type="radio" name="radio" value="saab">Saab
<input type="submit" name="submit" value="Get Selected Values" />

if (isset($_POST['submit'])) {
echo "You have selected :".$_POST['radio']; // Displaying Selected Value
// Get array values

$cars = array

I'm not really sure how to do the next bit. I've tried using something a bit like this, but I don't know how to set the first digit to the option that was selected in the radio field.

echo $cars[0][2];
echo $cars[0][3];

Any help would be appreciated.

EDIT: Result

If the first radio field was selected I want to output 22 and 18, since in the array that's what relates to volvo, the first radio option.

Answer Source

I'd probably use the radio value as a key in the array, like this:

$cars = array(
   'volvo' => array("volvo", 22, 18),
   'bmw' => array("bmw", 15, 13),
   'saab' => array("saab", 5, 2),

$selectedData = $cars[$_POST['radio']];
print $selectedData[1];
print $selectedData[2];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download