Aurora Aurora - 5 months ago 12
PHP Question

trying to merge multiple array in a loop codeigniter

Hey this Is a piece of code I have where I am getting result from the database and I am trying to combine all array I get from the database into one.
Here is the piece of code I am using

$result = array();
foreach($invoices as $invoice){
$result = $this->fee_page->readFeeRowsForInvoice($invoice['ORIG_ID']) ;
$result += array_merge($result);
}


The for each I am using is because my $invoices looks like this,
enter image description here SO I need to get both the arrays into the result as 2 arrays but just with [0] and 1.

Answer

Update your code as follow.

$result = array();
  foreach($invoices as $invoice){
     $invoice_rows = $this->fee_page->readFeeRowsForInvoice($invoice['ORIG_ID']) ;
     $result[] = $invoice_rows;
  }

We don't use + sign to merge two array. You simply add array values to other array using [].

Comments