I have an associative array in my php script to which I have to add elements depending on some conditions. My setup starts like this:
$arreglo = ["id" => $_POST["id"]];
When a certain condition is met, I have to add more elements so that the array has the information, for example, what is in $_POST["nombre"]
, $_POST["apellido"]
, etc. The goal is to use that associative array in an object PDO
to safely execute a query. How do I do it?
There are two possible ways to do it, quite simple.
As with a non-associative array, we can write:
Being the result (with hypothetical values)
array_merge
PHP functionAnother associative array can be created with the information we have. The function is used
array_merge
to "join" these two arrays, and the function returns a new array with the elements of both arrays.Being the content of $resulting_array
The information and main idea of this answer have been taken from this answer on the English site.