how to get the set of array_push data in real json format?

时间:2015-11-12 11:35:07

标签: php json restful-url

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 个答案:

答案 0 :(得分:0)

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