0

I have problem to combining two array, here my sample code

$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']
);

}

With this code, result is

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

I want to combine two array, So the output should be like this:

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

Please someone help me to find out the easiest way to solve this issue.

2

1 Answer 1

2

The proper way to fix this would be to change your database queries to one which would return all the information in a single query.

$data = $this->db->query("SELECT a.*, b.datapoints FROM table1 a, table2 b....");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.