尝试在循环codeigniter中合并多个数组

时间:2016-06-03 12:05:38

标签: php codeigniter

嘿这是我所拥有的一段代码,我从数据库获得结果,并且我正在尝试将我从数据库获得的所有数组合并为一个。 这是我正在使用的代码

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

我使用的每个是因为我的$发票看起来像这样, enter image description here所以我需要将两个数组中的数组作为2个数组,但只需要[0]和1

3 个答案:

答案 0 :(得分:0)

$result = array();  $append = array(); 

   foreach($invoices as $invoice){

     $append = $this->fee_page->readFeeRowsForInvoice($invoice['ORIG_ID']) ;
     $result[]= $append;
   }

答案 1 :(得分:0)

按以下步骤更新您的代码。

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

我们不使用 + 符号合并两个数组。您只需使用 [] 将数组值添加到其他数组。

答案 2 :(得分:0)

You can use array_push() function instead of array_merge() like this

$result = array();
foreach($invoices as $invoice){

 $temp = $this->fee_page->readFeeRowsForInvoice($invoice['ORIG_ID']) ;
 array_push($result,$temp);

}

我希望这对你有用。