2015-11-26 112 views
0

我有一个Json文件,我需要提取“tempm”我尝试使用不同的形式,但不能永远只得到该值。 enter image description here1] 1Json数组提取值PHP

$history1 = ("http://link_to_my_api"); 
echo $history1; 
$json_string = file_get_contents($history1); 
$parsed_json = json_decode($json_string); 
$location = $parsed_json->{'history'}->{'observations'}; 

var_dump($location[0]); 
+0

我认为这将是更好的将其转换为一个数组而不是对象。添加true作为json_decode的第二个参数,这使得它更容易导航。 – Daniel

+0

多数民众赞成它:D谢谢 –

+0

我发布它作为答案,将其标记为接受关闭该问题:) – Daniel

回答

0

通过添加作为真实的json_encode第二参数转换的JSON输出到阵列而不是一个对象,使一切更易于导航。

0

未经检验的,但我想这应该工作:

function ext($obj,$name){ 
    $ret=array(); 
    $rem=array(); 
    $f=function() use(&$rem,&$ret,$name){ 
     $v=reset($rem); 
     assert(NULL!==($key1=key($rem))); 
     unset($rem[$key1]); 
     foreach($v as $key2=>$value){ 
      if($key2==$name){$ret[]=$value;} 
      if(is_array($value) || is_object($value)) 
      { 
       $rem[]=$value; 
      } 
     } 
    }; 
    $rem[]=$obj; 
    while(!empty($rem)) 
    { 
     $f(); 
    }  
    return $ret; 
} 

$tempm_arr=ext($parsed_json,'tempm'); 
$tempm=$tempm_arr[0]; 

警告:这可能会导致与递归引用的对象/数组无限循环..