dqef7931 2015-11-12 11:35
浏览 117

如何以真正的json格式获取array_push数据集?

This is my rest apis data binding from database which is bined data with array notations..

        $result = array('status' => "Success");                    
        $result['data']['skill'] = array();
            foreach($skill->processApi('user_skills',true) as $row_data){                         
                    array_push($result['data']['skill'],$row_data);                        
            }

        $result['data']['user_projects'] = array();
            foreach($project->processApi('user_projects',true) as $row_data){                         
                    array_push($result['data']['user_projects'],$row_data);                        
            }

        $result['data']['user_experience'] = array();
            foreach($experience->processApi('user_experience',true) as $row_data){                         
                    array_push($result['data']['user_experience'],$row_data);                        
            }

        $result['data']['user_education'] = array();
            foreach($education->processApi('user_education',true) as $row_data){                         
                    array_push($result['data']['user_education'],$row_data);                        
            }

        $result['data']['user_basic_details'] = array();
            foreach($basic_info->processApi('user_basic_details',true) as $row_data){                         
                    array_push($result['data']['user_basic_details'],$row_data);                        
            }

        $result['data']['user_contact_details'] = array();
            foreach($contact_info->processApi('user_contact_details',true) as $row_data){                         
                    array_push($result['data']['user_contact_details'],$row_data);                        
            }

        $this->response($this->json($result), 200);

It gives json data as like bellow which is contained array notation([,]) because of array_push method in php

{
  "status": "Success",
  "data": {
    "skill": [
      {
        "skill_id": "2",
        "skill_title": "Sports"
      }
    ],
    "user_projects": [
      {
        "project_title": "workless",
        "project_id": "1",
        "date_start": "2015-08-28",
        "date_end": "2016-01-23",
        "project_description": "online recruitment service",
        "is_ongoing": "1"
      }
    ],
    "user_experience": [
      {
        "experience_id": "5",
        "job_title": "Software Engineer",
        "job_des": "Currently developing a data locality aware virtual machine placement strategy in cloud based Hadoop deployments. ResponsibilitiesReview and research on large-scale data processing platforms, data analytic tools and algorithms, streaming process, and data management",
        "company_name": "Schrdinger",
        "date_start": "2015-08-28",
        "date_end": "2016-01-23",
        "current_work": "1"
      }
    ],
    "user_education": [
      {
        "coun_name": "Afghanistan",
        "uni_title": "UOR",
        "uni_logo": null,
        "school": "DS School",
        "grade": "12",
        "description": "Science Stream",
        "field_studied": "field_studied",
        "added_date": "2015-09-05 14:00:03",
        "date_start": "2015-08-28",
        "date_end": "2016-02-23",
        "degree": "Computer Science"
      }
    ],
    "user_basic_details": [
      {
        "basic_details": "With degree and 2 years experiences.",
        "user_fname": "Mohamed",
        "user_sname": "Nifras",
        "user_im": "nifrasbcs",
        "bday": "1991-12-29",
        "gender": "1",
        "marital_status": "1"
      }
    ],
    "user_contact_details": [
      {
        "user_email": "nifrasnx@yahoo.com",
        "user_address_1": "No 32, New Market Road",
        "user_address_2": "Oddamavadi-01",
        "user_im": "nifrasbcs",
        "phone_mobile": "752786188",
        "phone_land": "652257153"
      }
    ]
  }
}

how to get json data without this array([,]) notation ,it'l be like

{
  "status": "Success",
  "data": {
    "skill": 
      {
        "skill_id": "2",
        "skill_title": "Sports"
      }
    ,
    "user_projects": 
      {
        "project_title": "workless",
        "project_id": "1",
        "date_start": "2015-08-28",
        "date_end": "2016-01-23",
        "project_description": "online recruitment service",
        "is_ongoing": "1"
      }
    ,
    "user_experience": 
      {
        "experience_id": "5",
        "job_title": "Software Engineer",
        "job_des": "Currently developing a data locality aware virtual machine placement strategy in cloud based Hadoop deployments. ResponsibilitiesReview and research on large-scale data processing platforms, data analytic tools and algorithms, streaming process, and data management",
        "company_name": "Schrdinger",
        "date_start": "2015-08-28",
        "date_end": "2016-01-23",
        "current_work": "1"
      }
    ,
    "user_education": 
      {
        "coun_name": "Afghanistan",
        "uni_title": "UOR",
        "uni_logo": null,
        "school": "DS School",
        "grade": "12",
        "description": "Science Stream",
        "field_studied": "field_studied",
        "added_date": "2015-09-05 14:00:03",
        "date_start": "2015-08-28",
        "date_end": "2016-02-23",
        "degree": "Computer Science"
      }
    ,
    "user_basic_details": 
      {
        "basic_details": "With degree and 2 years experiences.",
        "user_fname": "Mohamed",
        "user_sname": "Nifras",
        "user_im": "nifrasbcs",
        "bday": "1991-12-29",
        "gender": "1",
        "marital_status": "1"
      }
    ,
    "user_contact_details": 
      {
        "user_email": "nifrasnx@yahoo.com",
        "user_address_1": "No 32, New Market Road",
        "user_address_2": "Oddamavadi-01",
        "user_im": "nifrasbcs",
        "phone_mobile": "752786188",
        "phone_land": "652257153"
      }

  }
}

how to achieve it please help ?

  • 写回答

1条回答 默认 最新

  • doucheng9634 2015-11-12 11:53
    关注

    In JSON the syntax {} denotes a Class and [] denotes an array, so if you want to create the result you are after, you need to create the data in an approprite object initially i.e. a class or an array.

    You can simply create a class using the PHP stdClass() so as a starter to get you going you could do

    $result = new stdClass();
    
    $result->status = 'success';
    
    $result->data = new stdClass();
    
    $result->data->skill = array();
    foreach($skill->processApi('user_skills',true) as $row_data) {                         
        $result->data->skill[] = $row_data;
    }
    
    $result->data->user_projects = array();
    foreach($project->processApi('user_projects',true) as $row_data) {                         
        $result->data->user_projects[] = $row_data;
     }
    
    . . .
    
    . . .
    
    $this->response($this->json($result), 200);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导