0

I have a problem, namely, I do not know how to put new data into my array using the array_push() function in the foreach loop (to read database data). Code:

$result = array();
$i = 0;

#$rows - data from the database
foreach($res as $rows){
    $result[$i] = ['aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff'];
    array_push($result[$i], ['gg' => 'hh', 'ii' => 'jj']);

    $i++;
}

#The expected result:
#Array('aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff', 'gg' => 'hh', 'ii' => 'jj');

#Reality:
#Array(0 => ['gg' => 'hh', 'ii' => 'jj'], 'aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff');

Thank you in advance for help.

Trawlr
  • 149
  • 2
  • 8

1 Answers1

0

You have to do it like below:-

<?php

$result[] = ['aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff'];
$result = array_merge($result[0], ['gg' => 'hh', 'ii' => 'jj']);

print_r($result);

https://eval.in/850034

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98