How do I add a new value in an array?

0

How do I add new values in an array? For example, I have:

foreach ($Read->getResult() as $PDT):  
$pdt_promo[] = $PDT;

And out of the foreach I give a var_dump ($ pdt_promo), with the data I get from the database:

    array (size=2)
  0 => 
    array (size=8)
      'pdt_id' => string '16' (length=2)
      'pdt_name' => string 'body-fitness-army-by011-4' (length=25)
      'pdt_title' => string 'Body Fitness Army BY011/4' (length=25)
      'pdt_cover' => string 'images/2017/10/16-body-fitness-army-by011-4-1508204418.jpg' (length=58)
      'pdt_offer_price' => string '20.00' (length=5)
      'pdt_offer_start' => string '2018-04-11 10:00:00' (length=19)
      'pdt_offer_end' => string '2018-08-30 10:00:00' (length=19)
      'pdt_price' => string '39.90' (length=5)
  1 => 
    array (size=8)
      'pdt_id' => string '17' (length=2)
      'pdt_name' => string 'body-fitness-ocean-drive-oxbd1101' (length=33)
      'pdt_title' => string 'Body Fitness Ocean Drive OXBD1101' (length=33)
      'pdt_cover' => string 'plataforma-ecommerce/images/2018/02/17-body-fitness-ocean-drive-oxbd1101-1519218895.jpg' (length=87)
      'pdt_offer_price' => string '29.90' (length=5)
      'pdt_offer_start' => string '2018-02-27 10:00:00' (length=19)
      'pdt_offer_end' => string '2018-04-27 23:00:00' (length=19)
      'pdt_price' => string '290.90' (length=6)

But I wanted to add new values that come from a variable below pdt_price

Var_dump desired:

array (size=2)
  0 => 
    array (size=8)
      'pdt_id' => string '16' (length=2)
      'pdt_name' => string 'body-fitness-army-by011-4' (length=25)
      'pdt_title' => string 'Body Fitness Army BY011/4' (length=25)
      'pdt_cover' => string 'images/2017/10/16-body-fitness-army-by011-4-1508204418.jpg' (length=58)
      'pdt_offer_price' => string '20.00' (length=5)
      'pdt_offer_start' => string '2018-04-11 10:00:00' (length=19)
      'pdt_offer_end' => string '2018-08-30 10:00:00' (length=19)
      'pdt_price' => string '39.90' (length=5)
      'Outrovalor' => valor da minha variavel
      'Maisum' => outra variavel.. e por assim vai
  1 => 
    array (size=8)
      'pdt_id' => string '17' (length=2)
      'pdt_name' => string 'body-fitness-ocean-drive-oxbd1101' (length=33)
      'pdt_title' => string 'Body Fitness Ocean Drive OXBD1101' (length=33)
      'pdt_cover' => string 'plataforma-ecommerce/images/2018/02/17-body-fitness-ocean-drive-oxbd1101-1519218895.jpg' (length=87)
      'pdt_offer_price' => string '29.90' (length=5)
      'pdt_offer_start' => string '2018-02-27 10:00:00' (length=19)
      'pdt_offer_end' => string '2018-04-27 23:00:00' (length=19)
      'pdt_price' => string '290.90' (length=6)
      'Outrovalor' => valor da minha variavel
      'Maisum' => outra variavel.. e por assim vai

Using array_push, the result is this:

 array (size=4)
  0 => 
    array (size=8)
      'pdt_id' => string '16' (length=2)
      'pdt_name' => string 'body-fitness-army-by011-4' (length=25)
      'pdt_title' => string 'Body Fitness Army BY011/4' (length=25)
      'pdt_cover' => string 'images/2017/10/16-body-fitness-army-by011-4-1508204418.jpg' (length=58)
      'pdt_offer_price' => string '20.00' (length=5)
      'pdt_offer_start' => string '2018-04-11 10:00:00' (length=19)
      'pdt_offer_end' => string '2018-08-30 10:00:00' (length=19)
      'pdt_price' => string '39.90' (length=5)
  1 => 
    array (size=2)
      'laranja' => int 10
      'morango' => int 11
  2 => 
    array (size=8)
      'pdt_id' => string '17' (length=2)
      'pdt_name' => string 'body-fitness-ocean-drive-oxbd1101' (length=33)
      'pdt_title' => string 'Body Fitness Ocean Drive OXBD1101' (length=33)
      'pdt_cover' => string 'plataforma-ecommerce/images/2018/02/17-body-fitness-ocean-drive-oxbd1101-1519218895.jpg' (length=87)
      'pdt_offer_price' => string '29.90' (length=5)
      'pdt_offer_start' => string '2018-02-27 10:00:00' (length=19)
      'pdt_offer_end' => string '2018-04-27 23:00:00' (length=19)
      'pdt_price' => string '290.90' (length=6)
  3 => 
    array (size=2)
      'laranja' => int 10
      'morango' => int 11

Code:

$cesta = array("laranja" => 012, "morango" => 013);
        array_push($pdt_promo, $cesta);
    
asked by anonymous 12.04.2018 / 17:40

1 answer

3

Just do the union of the two arrays : what you already have and what has the values you want to add:

foreach ($Read->getResult() as $PDT) {
  $pdt_promo[] = array_merge($PDT, ['Outrovalor' => '...', 'Maisum' => '...']);
}

This will have the expected result.

    
12.04.2018 / 17:49