笨结合在foreach循环×2个阵列

我有问题要结合两个阵列,在这里我的示例代码笨结合在foreach循环×2个阵列

$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...."); 

以上是 笨结合在foreach循环×2个阵列 的全部内容, 来源链接: utcz.com/qa/257988.html

回到顶部