lingxueyu
V2EX  ›  PHP

一个新手问题——怎么指定返回的 json 参数

  •  
  •   lingxueyu · Apr 9, 2015 · 3889 views
    This topic created in 4098 days ago, the information mentioned may be changed or developed.
    from:
    {
    "status": "success",
    "total": 1,
    "data": [
    {
    "id": 1,
    "type": 0,
    "logo": "1428470471LLnfH.jpg",
    "name": "123",
    "profile": "",
    "city": "",
    "address": "",
    "location": "",
    "email": "[email protected]";,
    "phone": "123456",
    "website": "",
    "weibo": "",
    "service": "",
    "tags": "",
    "user_count": 3,
    "thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"
    }
    ]
    }
    to:
    {
    "status": "success",
    "total": 1,
    "data": [
    {
    "id": 1,
    "logo": "1428470471LLnfH.jpg",
    "name": "123",
    "email": "[email protected]";,
    "phone": "123456",
    "tags": "",
    "user_count": 3,
    "thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"
    }
    ]
    }
    刚接触php用laravel半个月了,找不到类似ruby的grape entity这种好用的gem
    3 replies    2015-04-09 21:15:29 +08:00
    kevin8096
        1
    kevin8096  
       Apr 9, 2015
    $obj = json_decode($jsonData) ;
    $obj->data
    ??
    kevin8096
        2
    kevin8096  
       Apr 9, 2015
    看错了。抱歉
    feiyuanqiu
        3
    feiyuanqiu  
       Apr 9, 2015   ❤️ 1
    需要先转成数组或对象处理之后再转回来吧?直接出结果的函数好像没有

    $from = '{"status": "success","total": 1,"data": [{"id": 1,"type": 0,"logo": "1428470471LLnfH.jpg","name": "123","profile": "","city": "","address": "","location": "","email": "[email protected]","phone": "123456","website": "","weibo": "","service": "","tags": "","user_count": 3,"thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"}]}';
    $from = json_decode($from, true);

    array_walk($from['data'], function (&$item) {
    $picks = array(
    'id' => '',
    'logo' => '',
    'name' => '',
    'email' => '',
    'phone' => '',
    'tags' => '',
    'user_count' => '',
    'thumb_logo' => '',
    );
    $item = array_intersect_key($item, $picks);
    });

    $from = json_encode($from);
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2685 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:26 · PVG 18:26 · LAX 03:26 · JFK 06:26
    ♥ Do have faith in what you're doing.