add a constant to a string -php

1

I would like to know if it is possible to add a constant String to another String .

Let me explain; I have a cycle that calls me 'X' number for a series:

  

53148

The first three numbers are a series of fecha 06 and the last two of fecha 03

  

date 06: 531 and date 03: 48

This series of numbers may have 'X' more dates.

I require that when the date changes I can add a String '0,' This to be able to make a change of internal category of the program the final result that I hope would be:

  

5310,40,8

Separating the dates:

  

date 06: 531 and date 03: 0,40,8

Code:
 foreach ($inventario as $x => $items ) {

     foreach ($items as $y => $descripcion) {
       echo $descripcion['stock'];


     }  
} 

I'm getting the data from an inventory table.

I know I must concatenate the String when it comes out of the second foreach ; But I could not know how to add that String , understanding that they can change the date several times.

//$Constante = '0,';
     foreach ($inventario as $x => $items ) {

         foreach ($items as $y => $descripcion) {
            //$str = echo $descripcion['stock'];   

         }    
         //$str +=$Constante 
    } 
    
asked by Andres Felipe Diaz 07.06.2018 в 03:52
source

1 answer

1

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.

        
    answered by 07.06.2018 / 14:06
    source