Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将json转换为对象php_Php_Json_Codeigniter - Fatal编程技术网

将json转换为对象php

将json转换为对象php,php,json,codeigniter,Php,Json,Codeigniter,我刚学过php 我有一个单独的数据库表,我想将该表与JSON输出结合起来,但结果输出是错误的,如何将我的JSON更改为正确且易于使用的JSON 谢谢 json输出: [ [ { "id_service": "3", "reference_number": "", "tracking_number": "RJC-0000-0001", "kd_inbound": "INB-1000-0001", "tgl_i

我刚学过php

我有一个单独的数据库表,我想将该表与JSON输出结合起来,但结果输出是错误的,如何将我的JSON更改为正确且易于使用的JSON

谢谢

json输出:

[
 [
    {
        "id_service": "3",
        "reference_number": "",
        "tracking_number": "RJC-0000-0001",
        "kd_inbound": "INB-1000-0001",
        "tgl_inbound": "2019-11-07 00:00:00",
        "status_inb": "1"
    }
 ],
 [
    {
        "id_service": "3",
        "reference_number": "",
        "kd_outbag": "BAG-1468-0002",
        "tanggal_outbag": "2019-11-07 00:00:00",
        "status_outbag": "1"
    }
 ],
 [
    {
        "id_service": "3",
        "reference_number": "",
        "kd_outbound": "OTB-1826-0001",
        "tgl_outbound": "2019-11-07 17:04:49",
        "status_otb": "1"
    }
 ],
]
这是我的密码

 public function awb_get() {
    $id = $this->get('tracking_number');
    $res= array(      
      $this->M_tarif->tampil_status_inbound($id),
      $this->M_tarif->tampil_status_otboundbag($id),
      $this->M_tarif->tampil_status_otboundori($id),
      $this->M_tarif->tampil_status_indes($id),
      $this->M_tarif->tampil_status_outdes($id),
      $this->M_tarif->tampil_status_runsheet($id),
      $this->M_tarif->tampil_db_service_status($id)
    );
    $this->response($res, 200);
}
我想这样进去

{
"status": 200,
"error": false,
"awb": [
    {
        "tracking_number": "RJC-0000-0004",
        "status": "order",
        "tanggal": "2019-10-30"
    },
    {
        "tracking_number": "RJC-0000-0004",
        "status": "Inbound to origin",
        "tanggal": "2019-11-03"
    }
]
}

您的json响应不正确。 因此,在发送到函数之前,首先必须重置JSON。我制作了一个示例来重新排列JSON并在stdClass对象中转换它。请检查下面的代码以转换您的响应

$arr ='[
 [
    {
        "id_service": "3",
        "reference_number": "",
        "tracking_number": "RJC-0000-0001",
        "kd_inbound": "INB-1000-0001",
        "tgl_inbound": "2019-11-07 00:00:00",
        "status_inb": "1"
    }
 ],
 [
    {
        "id_service": "3",
        "reference_number": "",
        "kd_outbag": "BAG-1468-0002",
        "tanggal_outbag": "2019-11-07 00:00:00",
        "status_outbag": "1"
    }
 ],
 [
    {
        "id_service": "3",
        "reference_number": "",
        "kd_outbound": "OTB-1826-0001",
        "tgl_outbound": "2019-11-07 17:04:49",
        "status_otb": "1"
    }
 ],
]';

$result = str_replace(array('[',']','\n'), '',htmlspecialchars(json_encode($arr), ENT_NOQUOTES));
$str = preg_replace('/\\\"/',"\"", $result);

$json = '[';
$json .= substr($str, 2,-1); // Substring -1 character from the end of the json variable, this will be the trailing comma. 
$json .= ']';
$jsonData = preg_replace("/,(?!.*,)/", "", $json);

echo "<pre>";
print_r(json_decode($jsonData));
echo "</pre>";

你必须使用FORLOOP将其转换为数组谢谢,你能举个例子吗?你的问题有点不清楚,你想合并哪个表?你的JSON输出无效,有没有解决方案来生成正确的JSON输出?在这里,我只是用正确的格式将JSON响应转换为正确的格式。使用与演示json数组相同的代码。在$arr数组变量中有什么?与您在json响应中提到的相同?
Array
(
    [0] => stdClass Object
        (
            [id_service] => 3
            [reference_number] => 
            [tracking_number] => RJC-0000-0001
            [kd_inbound] => INB-1000-0001
            [tgl_inbound] => 2019-11-07 00:00:00
            [status_inb] => 1
        )

    [1] => stdClass Object
        (
            [id_service] => 3
            [reference_number] => 
            [kd_outbag] => BAG-1468-0002
            [tanggal_outbag] => 2019-11-07 00:00:00
            [status_outbag] => 1
        )

    [2] => stdClass Object
        (
            [id_service] => 3
            [reference_number] => 
            [kd_outbound] => OTB-1826-0001
            [tgl_outbound] => 2019-11-07 17:04:49
            [status_otb] => 1
        )

)