如何将数组推送到couchdb json文档中的特定数组对象

时间:2014-09-04 05:50:24

标签: php yii couchdb couchdb-futon

更新时,couchDB json文档有两个独立的数组。但我将一个数组推入特定的对象数组。

 {

"_id":"employee_skill_doc",
"_rev":"269-e99377ad5d9e746a9d5b4777e7a50e46",
 "skills":[
 {
  "skill_name": "Dassault Simulia",
  "skill_id": "En17",
  "skill_set_id": "c03",
  "skill_set": "CAE (Computer Aided Engineering)",
  "skill_sub_ID": "b02",
  "skill_cat_ID": "a01"
 }],
 "certification":[
 {
  "certification_type": "networking",
  "certification_company": "citrix",
  "certification_name": "citrix",
  "certified_year": "1992",
  "certified_valitidy": "01/2014",
  "marks":"90",
  "comments":"sample",
  "skill_id": "En17"
 }]
 }

这里提到了预期的输出,

{

"_id": "employee_skill_doc",
"_rev": "269-e99377ad5d9e746a9d5b4777e7a50e46",
"skills": [{
 "skill_name": "Dassault Simulia",
 "skill_id": "En17","skill_set_id": "c03",
 "skill_set": "CAE (Computer Aided Engineering)",
 "skill_sub_ID": "b02","skill_cat_ID": "a01",
 "certification": [ {
 "certification_type": "networking",
 "certification_company": "citrix","certification_name": "citrix",
 "certified_year": "1992",
 "certified_valitidy": "01/2014",
 "marks": "90",
 "comments": "sample",
 "skill_id": "En17"
  }]
}]

}

1 个答案:

答案 0 :(得分:0)

试试这个

<?php
$json = ' {

"_id":"employee_skill_doc",
"_rev":"269-e99377ad5d9e746a9d5b4777e7a50e46",
 "skills":[
 {
  "skill_name": "Dassault Simulia",
  "skill_id": "En17",
  "skill_set_id": "c03",
  "skill_set": "CAE (Computer Aided Engineering)",
  "skill_sub_ID": "b02",
  "skill_cat_ID": "a01"
 }],
 "certification":[
 {
  "certification_type": "networking",
  "certification_company": "citrix",
  "certification_name": "citrix",
  "certified_year": "1992",
  "certified_valitidy": "01/2014",
  "marks":"90",
  "comments":"sample",
  "skill_id": "En17"
 }]
 }';

 $arr = json_decode($json, true);
 $arr['skills'][0]['certification'] =  $arr['certification'];
 unset($arr['certification']);
 $json_new = json_encode($arr, JSON_PRETTY_PRINT);
 echo $json_new;
 ?>

输出:

{
"_id":"employee_skill_doc",
"_rev":"269-e99377ad5d9e746a9d5b4777e7a50e46",
"skills":[
    {
    "skill_name":"Dassault Simulia",
    "skill_id":"En17",
    "skill_set_id":"c03",
    "skill_set":"CAE (Computer Aided Engineering)",
    "skill_sub_ID":"b02","skill_cat_ID":"a01",
    "certification":[
        {
        "certification_type":"networking",
        "certification_company":"citrix",
        "certification_name":"citrix",
        "certified_year":"1992",
        "certified_valitidy":"01\/2014",
        "marks":"90","comments":
        "sample","skill_id":"En17"
        }]
    }]
}

<强> SEE DEMO