2012-02-27 9 views
6

これは私のBitcoin Minerワーカーにとってdeepbit.netが返すjsonです。私はワーカー配列にアクセスし、[email protected]ワーカーの統計情報を出力するためにループしています。 confirmed_reward、hashrate、ipa、およびpayout_historyにアクセスできますが、ワーカー配列の書式設定と出力に問題があります。PHPを使用した多次元JSON配列の処理

{ 
"confirmed_reward":0.11895358, 
"hashrate":236.66666667, 
"ipa":true, 
"payout_history":0.6, 
"workers": 
    { 
     "[email protected]": 
     { 
     "alive":false, 
     "shares":20044, 
     "stales":51 
     } 
    } 
} 

はあなたの助けをありがとう:)

答えて

15

私はあなたがjson_decode方法、などで与えた文字列をデコードしてきたと仮定...

$data = json_decode($json_string, TRUE); 
は、特定の労働者の統計情報にアクセスするには、ちょうど使用する...

$worker_stats = $data['workers']['[email protected]']; 

たとえば、あなたは一緒に行く...

$is_alive = $worker_stats['alive']; 

本当に簡単です。 )

3

あなたがjson_decodeを使用していないのはなぜ。

文字列を渡すと、文字列よりも簡単に使用するオブジェクト/配列が返されます。

は、より正確には:

<?php 
$aJson = json_decode('{"confirmed_reward":0.11895358,"hashrate":236.66666667,"ipa":true,"payout_history":0.6,"workers":{"[email protected]":{"alive":false,"shares":20044,"stales":51}}}'); 
$aJson['workers']['[email protected]']; // here's what you want! 
?> 
2
$result = json_decode($json, true); // true to return associative arrays 
            // instead of objects 

var_dump($result['workers']['[email protected]']); 
2

json_decodeを使用すると、JSON文字列から連想配列を取得できます。

$json = 'get yo JSON'; 
$array = json_decode($json, true); // The `true` says to parse the JSON into an array, 
            // instead of an object. 
foreach($array['workers']['[email protected]'] as $stat => $value) { 
    // Do what you want with the stats 
    echo "$stat: $value<br>"; 
} 

あなたの例では、次のようになります