dongxian3418 2015-12-16 11:29
浏览 44
已采纳

如何返回json对象的数组 - PHP

I have some some data that I need to send to frontend as array of json objects, it basicly should have same structure as this json:

[{"vin":"U5YFF24128L064909","0":"U5YFF24128L064909","case_id":"1462538","1":"1462538","claimnumber":"E140039698","2":"E140039698","platenumber":"5M47465","3":"5M47465","axrmrs_id":"2051707","4":"2051707","insurer_memberid":"MM-O-8AB24F6D-1","5":"MM-O-8AB24F6D-1","country":"CZ","6":"CZ","date_created":"2014-02-10 00:00:00","7":"2014-02-10 00:00:00","totalloss":"N","8":"N","lastcalc_manufacturer_code":"28","9":"28","lastcalc_model_code":"25","10":"25","lastcalc_submodel_code":"07","11":"07","audavin_triggered":"","12":"","accident_date":"2014-02-02 00:00:00","13":"2014-02-02 00:00:00","registration_date":"2008-04-28 00:00:00","14":"2008-04-28 00:00:00","manufacturing_year":"2008","15":"2008","spareparts":"26573.4","16":"26573.4","totalcosts":"42187.47","17":"42187.47","laborhours":"7.3","18":"7.3","laborcosts":"3577","19":"3577","calculationdate":"2014-02-22 23:02:00","20":"2014-02-22 23:02:00","paintlabor":"5995","21":"5995","paintmaterial":"6042.07","22":"6042.07","currency":"CZK","23":"CZK","manufacturer":"KIA","24":"KIA","model":"CEED (ED)","25":"CEED (ED)","submodel":"ACTIVE","26":"ACTIVE","orgName":"Global Expert","27":"Global Expert","textL":"BLATN\u00cdK P PLAKOV\u00c1N\u00cd NOV\u00ddCH D\u00cdL\u016e,DVE\u0158E P PLAK POVRCHU,P\u0158-N\u00c1RAZN\u00cdKLAKOV\u00c1N\u00cd NOV\u00ddCH D\u00cdL\u016e","28":"BLATN\u00cdK P PLAKOV\u00c1N\u00cd NOV\u00ddCH D\u00cdL\u016e,DVE\u0158E P PLAK POVRCHU,P\u0158-N\u00c1RAZN\u00cdKLAKOV\u00c1N\u00cd NOV\u00ddCH D\u00cdL\u016e","textE":null,"29":null},{"vin":"U5YFF24128L064909","0":"U5YFF24128L064909","case_id":"1468328","1":"1468328","claimnumber":"5M47465","2":"5M47465","platenumber":"5M47465","3":"5M47465","axrmrs_id":"2037572","4":"2037572","insurer_memberid":"","5":"","country":"CZ","6":"CZ","date_created":"2014-02-13 00:00:00","7":"2014-02-13 00:00:00","totalloss":"","8":"","lastcalc_manufacturer_code":"28","9":"28","lastcalc_model_code":"25","10":"25","lastcalc_submodel_code":"07","11":"07","audavin_triggered":"","12":"","accident_date":"0000-00-00 00:00:00","13":"0000-00-00 00:00:00","registration_date":"0000-00-00 00:00:00","14":"0000-00-00 00:00:00","manufacturing_year":"2008","15":"2008","spareparts":"25319.55","16":"25319.55","totalcosts":"41529.62","17":"41529.62","laborhours":"7.7","18":"7.7","laborcosts":"3850","19":"3850","calculationdate":"2014-02-23 23:02:00","20":"2014-02-23 23:02:00","paintlabor":"5995","21":"5995","paintmaterial":"6042.07","22":"6042.07","currency":"CZK","23":"CZK","manufacturer":"KIA","24":"KIA","model":"CEED (ED)","25":"CEED (ED)","submodel":"ACTIVE","26":"ACTIVE","orgName":"0","27":"0","textL":"BLAT","textE":null,"29":null}]

How I try it:

foreach ( $array ['result'] ['claim'] as $claim )
    {

        $data = array (
            "claimnumber" =>$claim['@attributes']['id'],
            "date_created" =>$claim['country'],
            "country" =>$claim['creation'],
            "currency" =>$claim ['specific']['currency'],
            "insurer_memberid" =>$claim['insurance']['id'],
            "laborcosts" =>$claim ['specific']['partsCost'],
            "model" =>$claim ['specific']['model_name'],
            "orgName" =>$claim['insurance']['name'],
            "paintLabor" =>$claim['specific']['paintmaterial'],
            "totalcosts" =>$claim ['assessment']['damage-value']
        );
        $result = array();
        $result[] = $data;
        $result[] = array_push($result, $this->convertToJson($result));
    }
    echo $result;

But this return just string "Array", is there some nice way how to create API which display array of objects?

Thanks for any advise :)

  • 写回答

4条回答 默认 最新

  • dqpc1845 2015-12-16 11:35
    关注

    Create the array before the loop, add the data within the loop, then json_encode and echo after the loop:

    $result = array();
    foreach ( $array ['result'] ['claim'] as $claim )
    {
    
        $data = array (
            "claimnumber" =>$claim['@attributes']['id'],
            "date_created" =>$claim['country'],
            "country" =>$claim['creation'],
            "currency" =>$claim ['specific']['currency'],
            "insurer_memberid" =>$claim['insurance']['id'],
            "laborcosts" =>$claim ['specific']['partsCost'],
            "model" =>$claim ['specific']['model_name'],
            "orgName" =>$claim['insurance']['name'],
            "paintLabor" =>$claim['specific']['paintmaterial'],
            "totalcosts" =>$claim ['assessment']['damage-value']
        );
        $result[] = $data;
    }
    echo json_encode($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码