Hullo :)
I have a multidimensional array that looks like this:
$things = array(
array('one'=>'red', 'two'=>'$85' , 'three'=>1),
array('one'=>'green', 'two'=>'$35' , 'three'=>0),
array('one'=>'blue', 'two'=>'$32' , 'three'=>0),
);
Various things are done with this array on my PHP page, including making a form with fields of names/IDs red, green and blue, like this:
<form>
<?php foreach ($things as $row) {
echo '<input type="hidden" name="' . $row['one'] . '" id="' . $row['one'] . '" value="0" />';
} ?>
<input type="submit" />
</form>
When the form is submitted, I want to put the $_REQUEST into a MySQL table with columns named after the field values, and this is where I'm having difficulty. This is my query:
INSERT INTO table_name (name, email, $fields_list)
VALUES ('$name', '$email', '$values_list')
"$fields_list" is just a comma-separated list of the field names (red, green, blue). "$values_list" is supposed to be a list of the $_REQUEST values and is constructed like this from "$colours":
$values_list = implode('\', \'', array_map(function ($entry) {
return $entry;
}, $colours));
What's in "$colours" is the issue. When I do this, it works:
$colours = array($_REQUEST["red"], $_REQUEST["green"], $_REQUEST["blue"]);
But I don't want to explicitly re-state the values in the array, as this is likely to change over time. I want to just have the array defined at the top of the page, and then have the rest of the functions work from that. So, I tried this, but it doesn't work:
$colour_list = implode(', ', array_map(function ($entry) {
return '$_REQUEST["'.$entry['one'].'"]';
}, $things));
$colours = array($colour_list);
If I var_dump this version of $colours, it's identical to the explicitly-stated version above.
Why doesn't it work? How can I make it work? Am I going about this the wrong way?
I think it has something to do with what order things are processed in - the PHP versus the REQUEST. I also think I am over-processing my arrays/variables, I have a feeling I can skip a step somewhere but am confused and can't work out where. Please help!
Thanks very much :)