Himanshu
Himanshu

Reputation: 51

How to convert my resultset into a grouped multi-dimensional array then a json string?

I have a table in my database with a structure and data like this:

---------------------------------------------
   id   |    type      |    name   |    age
---------------------------------------------
    1        F-A           jon          24
    2        F-A           roy          25
    3        F-E           robert       26
    4        F-E           sina         25

I want to group the data based on the type value to generate a specifically structured array to generate the following json:

{
  type:{
     "F-A": [
                {
                    "name": "jon",
                    "age": "24"
                    "id": "1"
                  },
                {
                    "name": "roy",
                    "age": "25",
                    "id": "2"
                }
           ],
"F-E":     [
                  {
                   "name": "robert",
                    "age": "26",
                    "id": "3"
                  },
                 {
                    "name": "sina",
                    "age": "25",
                    "id": "4"
                  },
         ]
      }
}

Notice that each unique type subarray contains a collection of row data associated with the value.

My model:

public function prescriptionData(){
    $table = 'prescription';
    $this->db->select('*');
    $this->db->from($table);
    $query=$this->db->get();
    $locations = [];
    //===   
    $val4=[];
    $this->db->select('type,name,age,id');
    $this->db->from($table);
    $query2=$this->db->get();
    Foreach($query2->result()   as  $k=>$va13){
        $val4[$k]= $va13;
    }
    foreach($query2->result_array() as $val){
        if(isset($val['type'])){
            $locations[type][$val['type']]=$val4;
        }else{
            $locations[type][$val['type']]= $val4;
        }
    }
    return $locations;        
}

My Controller:

public function prescription(){
    $userCount['result'] = $userCount1 = $this->Querydata->prescriptionData();
    if($userCount['result']>0){
        $validation = array(
            "responseCode" =>  $this->res = 200,
            "responseMessage" =>  $this->login = 'Training programs details successfully added',
            "data" =>  $this->data = $userCount['result'] );
        echo json_encode($validation);

This is my incorrect json response:

"type": {
         "F-A": [
                 {
                  "type": "F-A",
                  "name": "Jon",
                  "age": "24",
                  "id": "1"
                 },
                 {
                  "type": " F-A",
                  "name": "roy",
                  "age": "25",
                  "id": "2"
                 },
                 {
                  "type": "F-E",
                  "name": "robert",
                  "age": "26",
                  "id": "3"
                  },
                  {
                   "type": " F-E",
                   "name": "sina",
                   "age": "25",
                   "id": "4"
                  }
                 ],
         " F-A": [
                  {
                   "type": "F-A",
                   "name": "Jon",
                   "age": "24",
                   "id": "1"
                  },
// etc.

Upvotes: 0

Views: 63

Answers (1)

mickmackusa
mickmackusa

Reputation: 47863

Assuming that you are pre-sorting on the type column, you merely need to hard code keys type and the iterated value from the type column, then push the remaining row data as a subarray. When the loop finishes, encode the output array.

I'll use pretty_print to make the output easier to read, but you will not necessarily need to.

Code: (Demo)

$resultset = [
    ['id' => 1, 'type' => 'F-A', 'name' => 'jon', 'age' => 24],
    ['id' => 2, 'type' => 'F-A', 'name' => 'roy', 'age' => 25],
    ['id' => 3, 'type' => 'F-E', 'name' => 'robert', 'age' => 26],
    ['id' => 4, 'type' => 'F-E', 'name' => 'sina', 'age' => 25]
];

foreach ($resultset as $row) {
    $output['type'][$row['type']][] = ['name' => $row['name'], 'age' => $row['age'], 'id' => $row['id']];
}
echo json_encode($output, JSON_PRETTY_PRINT);

Output:

{
    "type": {
        "F-A": [
            {
                "name": "jon",
                "age": 24,
                "id": 1
            },
            {
                "name": "roy",
                "age": 25,
                "id": 2
            }
        ],
        "F-E": [
            {
                "name": "robert",
                "age": 26,
                "id": 3
            },
            {
                "name": "sina",
                "age": 25,
                "id": 4
            }
        ]
    }
}

Upvotes: 1

Related Questions