This question already has an answer here:
I have a method that inserts data into a table using an associative array that looks like this:
array(
"col1" => "value1",
"col2" => "value2",
"col3" => "value3",
);
If I use array_keys()
and array_values()
on the array passed in can I guarantee that the first item in each set, match the original array passed in?
// Note: validation excluded
$keys = array_keys($data); // $data is the array passed in
$values = array_values($data);
$q = array_pad(array(), count($data), "?");
$this->query("insert into `$this->table` (`" . implode("`,`", $keys) . "`) values (" . implode(",", $q) . ")", $values);
So, with that, can I guarantee that $keys[1]
and $values[1]
are the same key and value from the original array, or is it possible that they could be col3
and value1
?
Another way to say it, if i use array_combine($keys, $values)
will I get the original array key => value
pair back (item order excluded)?
I am worried that if this doesn't do what I am thinking that value2
may go into col3
instead of col2
or something like that....
</div>