簡體   English   中英

Codeigniter在foreach循環中合並兩個數組

[英]Codeigniter combine two array in foreach loop

我在合並兩個數組時遇到問題,這是我的示例代碼

$arr1 = [];
$data = $this->db->query("SELECT QUERY");

foreach ($data->result_array() as $row) {

$arr1[] = array(
   "type"                   => "column",
   "name"                   => $row['name'],
   "legendText"             => $row['name'],
   "showInLegend"           => true
);

}


$count = $this->db->query("SELECT QUERY");

foreach ($count->result_array() as $rows) {

$arr1[]["dataPoints"] = array(
   "label" => $rows['data']
);

}

使用此代碼,結果是

[
  {
    "type": "column",
    "name": "LA 1",
    "legendText": "LA 1",
    "showInLegend": true
  },
  {
    "dataPoints": {
      "label": "1"
    }
  }
]

我想合並兩個數組,所以輸出應如下所示:

[
  {
    "type": "column",
    "name": "LA 1",
    "legendText": "LA 1",
    "showInLegend": true,
    "dataPoints": [{
      "label": "1"
    }]
  }
]

請有人幫助我找出解決此問題的最簡單方法。

解決此問題的正確方法是將數據庫查詢更改為一個查詢,該查詢將在單個查詢中返回所有信息。

$data = $this->db->query("SELECT a.*, b.datapoints FROM table1 a, table2 b....");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM