json_decode使用

json_decode可以控制返回的json是数组还是obj

json_decode()

该函数用于将json文本转换为相应的PHP数据结构。下面是一个例子:

  $json = '{"foo": 12345}';

  $obj = json_decode($json);

  print $obj->{'foo'}; // 12345

通常情况下,json_decode()总是返回一个PHP对象,而不是数组。比如:

  $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

  var_dump(json_decode($json));

结果就是生成一个PHP对象:

  object(stdClass)#1 (5) {

    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)

  }

如果想要强制生成PHP关联数组,json_decode()需要加一个参数true:

  $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

  var_dump(json_decode($json),true);

结果就生成了一个关联数组:

  array(5) {

     ["a"] => int(1)
     ["b"] => int(2)
     ["c"] => int(3)
     ["d"] => int(4)
     ["e"] => int(5)

  }

sample:

function get_recommend_table_ex($dev_id)
{
    $url = 'http://192.168.1.175:8080/lct-server-1.0/mvc/recommend/get_recommend?device_id=555';
    $json = file_get_contents($url);
    $list=json_decode($json,true);
    return $list;
}


你可能感兴趣的:(数据结构,json,PHP,list,function,object)