如何使用php json

时间:2018-12-02 02:27:43

标签: php arrays json codeigniter multidimensional-array

我有两个表Car和Vendor

table: Car
--------------------------- 
| carID | carBrand        |
---------------------------
| 1     | Volvo           |
---------------------------
| 2     | BMW             |
---------------------------
| 3     | Mercedes        |
---------------------------

table: Vendor
----------------------------------------------------
| vendorID | vendorName        | telephone | carID |
----------------------------------------------------
| 1        | Jusoh Trading     | 12345     | 1     |
----------------------------------------------------
| 2        | Ibrahim and Sons  | 34566     | 1     |
----------------------------------------------------
| 3        | GMC Auto          | 566444    | 2     |
----------------------------------------------------
| 4        | Perodua Services  | 456666    | 3     |
----------------------------------------------------
| 5        | AMG Specs         | 5666      | 3     |
----------------------------------------------------

im使用sql查询(codeigniter)使用json产生输出。通过使用输出,我将创建一个图表,这是我的插件所需的数组格式。使用上面的sql&json编码,我的成功输出如下:

[
  {
    "carBrand": "Volvo",
    "carID": "1"
  },
  [
    {
      "vendorID": "1",
      "vendorName": "Jusoh Trading"
    },
    {
      "vendorID": "2",
      "vendorName": "Ibrahim and Sons"
    }
  ],
  {
    "carBrand": "BMW",
    "carID": "2"
  },
  [
    {
      "vendorID": "3",
      "vendorName": "GMC Auto"
    }
  ],
  {
    "carBrand": "Mercedes",
    "carID": "3"
  },
  [
    {
      "vendorID": "4",
      "vendorName": "Perodua Services"
    },
    {
      "vendorID": "5",
      "vendorName": "AMG Specs"
    }
  ]
]

我的代码生成输出:

$data = array();
$this->db->select("carBrand, carID");
$this->db->from('car');
$query = $this->db->get();

$result = $query->result();

foreach ($result as $row):
    $data[] = $row;

    $this->db->select("vendorID, vendorName");
    $this->db->from('vendor');
    $this->db->where('carID',$row->carID);
    $query = $this->db->get();
    $res = $query->result();

    $child = array();
        foreach ($res as $val):
            $child[] = $val;
        endforeach;

    array_push($data, $child);
endforeach;

echo json_encode($data);
exit;

但是我的问题是,地图插件需要一些不同的格式。我需要对输出格式进行一些更改。我需要的确切格式如下:

[
  {
    "carBrand": "Volvo",
    "vendorlist": [
    {
        "vendorID": "1",
        "vendorName": "Jusoh Trading"
    },
    {
        "vendorID": "2",
        "vendorName": "Ibrahim and Sons"
    }
    ]
  },
  {
    "carBrand": "BMW",
    "vendorlist": [
    {
        "vendorID": "3",
        "vendorName": "GMC Auto"
    }
    ]
  }
  # .. other data .. #
];

我需要删除“ carID”并添加“ vendorlist” ..我需要修改我的代码的建议。谢谢

1 个答案:

答案 0 :(得分:0)

$json = '[
  {
    "carBrand": "Volvo",
    "carID": "1"
  },
  [
    {
      "vendorID": "1",
      "vendorName": "Jusoh Trading"
    },
    {
      "vendorID": "2",
      "vendorName": "Ibrahim and Sons"
    }
  ],
  {
    "carBrand": "BMW",
    "carID": "2"
  },
  [
    {
      "vendorID": "3",
      "vendorName": "GMC Auto"
    }
  ],
  {
    "carBrand": "Mercedes",
    "carID": "3"
  },
  [
    {
      "vendorID": "4",
      "vendorName": "Perodua Services"
    },
    {
      "vendorID": "5",
      "vendorName": "AMG Specs"
    }
  ]
]';

$narr = [];
$arr = json_decode($json,true);

foreach($arr as $k => $v)
{

  if( $k%2 == 0)
  {
    $narr[] = ['carBrand' => $v['carBrand'] , 'vendorlist' =>  $arr[$k+1]];
  }
}

$new_json = json_encode($narr);