How do I add a new value to an array?

Asked

Viewed 39 times

0

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

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

And outside the foreach I give a var_dump($pdt_promo), with the data I receive 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 comes from a variable below pdt_price

Desired var_dump:

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 push array_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);
  • 1

    One of the options would be array_push. Or simply $data[$key] = $value;.

  • @RBZ I already tried using array_push and array_merge, but I was not successful.. could you give me an example?

  • 1

    Sorry, I didn’t understand that it was to unite the two !

  • Thank you @RBZ :)

1 answer

3


Just make 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' => '...']);
}

Thus will have the expected result.

  • 2

    @Williamalvares if the answer solved, click the green beside it to mark as solution, so help other visitors

Browser other questions tagged

You are not signed in. Login or sign up in order to post.