从PHP中的JSON解码数据中提取FIRST结果

从PHP中的JSON解码数据中提取FIRST结果

问题描述:

I have a script:

$json_url = "https://blockchain.info/ticker";
$json = file_get_contents($json_url);
$json=str_replace('},

]',"}

]",$json);
$data = json_decode($json);

echo "$" . ($data->USD->last);

This works perfect!

Now I have a second version of this:

$json_url3 = "https://blockchain.info/da/unconfirmed-transactions?format=json";
$json3 = file_get_contents($json_url3);
$json3=str_replace('},

]',"}

]",$json3);
$data3 = json_decode($json3);
echo "Latest hash: " . ($data3->txs->hash) . "<br>";

Why does the send script not work? I have a $json2 running without any issues with another API call as well.

我有一个脚本: p>

  $ json_url =“https  ://blockchain.info/ticker“; 
 $ json = file_get_contents($ json_url); 
 $ json = str_replace('},
 
]',”} 
 
]“,$ json)  ; 
 $ data = json_decode($ json); 
 
echo“$”。  ($ data-&gt; USD-&gt; last); 
  code>  pre> 
 
 

这很完美! p>

现在我有一秒钟 版本: p>

  $ json_url3 =“https://blockchain.info/da/unconfirmed-transactions?format=json";
$json3 = file_get_contents($ json_url3  ); 
 $ json3 = str_replace('},
 
]',“} 
 
]”,$ json3); 
 $ data3 = json_decode($ json3); 
echo“Latest hash:”  。  ($ data3-&gt; txs-&gt;哈希)。  “&lt; br&gt;”; 
  code>  pre> 
 
 

为什么发送脚本不起作用? 我运行了 $ json2 code>,没有任何其他API调用问题。 p> div>

Variable txs is an array of objects.

Your last line should be written as:

echo "Latest hash: " . ($data3->txs[0]->hash) . "<br>";

It outputs:

Latest hash: fd37b1ddfbe08d485a62bb3aeb7c9088e7dd3a352ac9e9e8eb6f170a9b4210cd

This code worked :)

$json_url3 = "https://blockchain.info/da/unconfirmed-transactions?format=json";
$json3 = file_get_contents($json_url3);
$json3=str_replace('},

]',"}

]",$json3);
$data3 = json_decode($json3);
echo "Latest hash: " . ($data3->txs[0]->hash) . "<br>";

txs[0]

I assume the issue was that there is more than 1(one) element in the array