如何将结果集转换为分组的多维数组,然后转换为json字符串?

时间:2018-08-15 13:21:48

标签: php arrays json api

我的数据库中有一个表,其结构和数据如下:

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

我想基于type值对数据进行分组,以生成特定结构的数组,以生成以下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"
                  },
         ]
      }
}

请注意,每个唯一类型子数组都包含与该值关联的行数据的集合。

我的模特:

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;        
}

我的控制器:

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);

这是我不正确的json响应:

"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.

1 个答案:

答案 0 :(得分:1)

假设您正在type列上进行预排序,则只需要对type硬键和type列中的迭代值进行硬编码,然后推送其余的行数据作为子数组。循环结束后,对输出数组进行编码。

我将使用pretty_print使输出更易于阅读,但您不一定需要这样做。

代码:(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);

输出:

{
    "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
            }
        ]
    }
}