Hello everyone first of all thanks for your answers and comments, I want to achieve the following in each iteration of a forEach loop I want to store the result in a variable so that at the end of the loop my variable must have all the results of the foreach loop I write the caught that I have done to explain myself better.
<?php
$request = [1,"darinel cigarroa","24","2021-12-16 00:00:00","2021-12-16 00:00:00"];
$consulta = null;
$colums = ["id","nombre","edad","created_at","updated_at"];
foreach ($colums as $campo)
{
foreach($request as $rows){
$consulta = $campo . '=' . $rows;
}
}
var_dump($consulta);
The result of this cycle is the following:
"updated_at=2021-12-16 00:00:00"
And what I want to generate is something like the following:
id=1, nombre='darinel cigarroa', edad='24', created_at='16/12/21', updated_at='16/12/21'
How can I do a push type of each iteration in each loop to get to the result? I thank you in advance for your answers and comments, any kind of help is useful and appreciated. The following code is Rodrigo's response, it achieves the purpose of the question, however, it repeats the field for each column, this is the code:
$request = [1,"darinel cigarroa","24","2021-12-16 00:00:00","2021-12-16 00:00:00"];
$consulta = "";
$colums = ["id","nombre","edad","created_at","updated_at"];
foreach ($colums as $campo)
{
foreach($request as $rows){
$consulta .= $campo . '=' . $rows . ", ";
}
}
# El ultimo elemento temrinara en ", "
# Lo eliminaremos usando preg_replace
$consulta = preg_replace("/\s*\,\s*$/", "", $consulta);
print($consulta);
The result is as follows:
id=1, id=darinel cigarroa, id=24, id=2021-12-16 00:00:00, id=2021-12-16 00:00:00, nombre=1, nombre=darinel cigarroa, nombre=24, nombre=2021-12-16 00:00:00, nombre=2021-12-16 00:00:00, edad=1, edad=darinel cigarroa, edad=24, edad=2021-12-16 00:00:00, edad=2021-12-16 00:00:00, created_at=1, created_at=darinel cigarroa, created_at=24, created_at=2021-12-16 00:00:00, created_at=2021-12-16 00:00:00, updated_at=1, updated_at=darinel cigarroa, updated_at=24, updated_at=2021-12-16 00:00:00, updated_at=2021-12-16 00:00:00
This is already clearer to me if I can solve it right away I share the answer, thank you very much for contributing to my problem.
(edited answer)
Indeed, the indices are repeated, but because you use two loops, however, if the two arrays you query (both
$request
)$colums
have the same number of elements, you don't need two loops, just usefor
instead offoreach
, to get the index that we can use to obtain the column that corresponds to that valueIt seems to me that what you're looking for is the result of applying
array_combine()
which:Doing this:
$consulta
will be an associative array, whose keys will be whatever is in$columns
(I have corrected, one was missingn
) and the values will be whatever is in$request
.Full example:
Departure:
Having the data organized, you can read
$consulta
the way you want, considering that it is an array.For example, if it's about constructing a statement
UPDATE
, you could do something like this:Departure:
Here we have simply taken advantage of the structure of the array to create the
SET
query partUPDATE
and, since a comma would be left over inside the loop (that of the last value in the array), we remove it withrtrim()
. In addition, we usesprintf()
, which in these cases helps a lot to simplify the code, creating dynamic content.Be that as it may, this is by way of example, so that you understand that using the right tools you can create any content dynamically very easily. Really, for this case the best thing would be to create a prepared query , where the adaptation would be even easier (using the appropriate tools), being also the recommended practice at the level of data security and code optimization.