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 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染