The concatenation operator is .
. You should do
$str .= $constante
But in the loop that shows you have two errors:
$str = echo $otra_cosa;
is wrong. If you want to step on the value of $str
you should put $str = $otra_cosa;
If you step on the value of $str
in each iteration, at the end of the main loop you will only have the last value plus the constant, so you may want to put $str .= $otra_cosa;
and initialize $str
as an empty string at the beginning of the script.
I think what you want to do is:
$Constante = '0,';
$str = '';
foreach ($inventario as $x => $items ) {
foreach ($items as $y => $descripcion) {
$str .= $descripcion['stock'];
}
$str .= $Constante
}
However, your question does not explain exactly what you want to achieve. If the table in your database has the structure you show in the image, it would give rise to an array of the type:
$inventario = [
['producto'=>'vodka absolut', 'fecha'=>'2018-06-01', 'stock'=>1],
['producto'=>'tequila olmeca', 'fecha'=>'2018-06-01', 'stock'=>4],
['producto'=>'whisky royal', 'fecha'=>'2018-06-01', 'stock'=>1],
['producto'=>'ron silver', 'fecha'=>'2018-06-01', 'stock'=>2],
['producto'=>'chimbombo', 'fecha'=>'2018-06-01', 'stock'=>2],
];
So the nested loop would not be necessary, since within the first foreach
you could already access the stock using $items['stock']
.
If you are instead getting a nested array of the type:
$inventario = [
[
['producto' => 'vodka absolut', 'fecha' => '2018-06-01', 'stock' => 1],
['producto' => 'vodka absolut', 'fecha' => '2018-06-02', 'stock' => 3]
],
[
['producto' => 'tequila olmeca', 'fecha' => '2018-06-01', 'stock' => 2],
['producto' => 'tequila olmeca', 'fecha' => '2018-06-02', 'stock' => 4]
],
[
['producto' => 'whisky royal', 'fecha' => '2018-06-01', 'stock' => 2],
['producto' => 'whisky royal', 'fecha' => '2018-06-02', 'stock' => 1]
],
[
['producto' => 'ron silver', 'fecha' => '2018-06-01', 'stock' => 5],
['producto' => 'ron silver', 'fecha' => '2018-06-02', 'stock' => 2]
],
[
['producto' => 'chimbombo', 'fecha' => '2018-06-01', 'stock' => 2],
['producto' => 'chimbombo', 'fecha' => '2018-06-02', 'stock' => 6]
],
];
Then the nested loop makes sense.
If we assume that you want to generate a chain with the concatenation of each stock plus the constant, then when doing:
$constante = '0,';
$str='';
foreach ($inventario as $x => $items) {
foreach ($items as $y => $descripcion) {
$str.=$descripcion['stock'];
}
$str .= $constante;
}
The final value of $str
would be: 130,240,210,520,260,
But you say that the expected value would be of type numero,numero,numero
, so adding the constant will always cause $str
to end in 0,
. I imagine then what you could do:
$constante = '0,';
$str=[];
foreach ($inventario as $x => $items) {
foreach ($items as $y => $descripcion) {
$str[]=$descripcion['stock'];
}
}
$str = implode($constante, $str);
In which case $ str would have the value 10,30,20,40,20,10,50,20,20,6
.
Now, I still can not imagine the usefulness of this loop. I think if what you really want is to add a codigo
column to each row of the final table, you'll have to show us the content of $inventario
and the expected value of your final table.