Это мое остальное привязка данных API из базы данных, которая представляет собой данные с массивами.
$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);
Это дает данные JSON, как показано ниже, который содержит обозначение массива ([,]) из-за метода array_push в 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": "[email protected]",
"user_address_1": "No 32, New Market Road",
"user_address_2": "Oddamavadi-01",
"user_im": "nifrasbcs",
"phone_mobile": "752786188",
"phone_land": "652257153"}
]
}
}
как получить данные JSON без этой записи массива ([,]), это будет как
{
"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": "[email protected]",
"user_address_1": "No 32, New Market Road",
"user_address_2": "Oddamavadi-01",
"user_im": "nifrasbcs",
"phone_mobile": "752786188",
"phone_land": "652257153"}
}
}
как этого добиться пожалуйста помогите?
В JSON синтаксис {}
обозначает класс и []
обозначает массив, поэтому, если вы хотите создать результат, к которому вы стремитесь, вам необходимо сначала создать данные в подходящем объекте, то есть в классе или массиве.
Вы можете просто создать класс, используя PHP stdClass()
так что для начала вы могли бы сделать
$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);
Других решений пока нет …