2013-12-17 30 views
0

我從網站拉json並使用json_decode函數將其放入數組中。然而,數組沒有被定義,所以我不能抓住第一個數組下面定義的索引。Json解碼,但沒有指定數組

這是我的json在運行解碼之後的一個示例。

Array 
    (
     [username] => username 
     [confirmed] => 0.00 
     [estimate] => 0.00 
     [total] => 0000 
     [history] => 0000.0000 
     [round] => 0000 
     [workers] => Array 
      (
       [username.1] => Array 
        (
         [alive] => 1 
         [rate] => 0000 
        ) 

這是我用來得到的代碼。

$json = file_get_contents("http://imareal.website", true); //getting the file content 
    $decode = json_decode($json, true); 

這樣我就可以在最上面的數組中訪問任何與

echo "Username: " .$decode["username"]; 

,但一旦我需要去低則[工人]它未能找到索引。

有什麼辦法可以在解碼過程中或解碼後定義數組?或者我可以告訴它以某種方式看起來更低?

更新: 好吧,運行var_dump($ decode ['workers']); 給我:

array(5) { ["username.1"]=> array(2) { ["alive"]=> string(1) "1" ["rate"]=> string(4) "0000" } ["username.2"]=> array(2) { ["alive"]=> string(1) "1" ["rate"]=> string(4) "0000" } ["username.3"]=> array(2) { ["alive"]=> string(1) "1" ["rate"]=> string(3) "0000" } ["username.10"]=> array(2) { ["alive"]=> string(1) "0" ["rate"]=> string(1) "0000" } ["username.11"]=> array(2) { ["alive"]=> string(1) "1" ["rate"]=> string(3) "643" } } 

當我嘗試使用以下任何線的我得到一個非法或未定義的偏移或不確定的指數。

echo $decode[5]->alive->value; 
    echo $decode[2]->alive->value; 
    echo "Username.1: " .$decode["username.1"]["alive"]; 
    echo "Username.1: " .$decode["2"]["alive"]; 
    echo "Username.1: " .$decode["5"]["alive"]; 
+1

你在做什麼嘗試抓取信息比工人低「? –

+3

''foreach($ decode ['workers'] as $ k => $ v){...}' – 2013-12-17 20:38:26

+0

你必須var_dump($ decode ['workers'])不回顯,因爲它不是字符串。如果你想訪問任何元素,你必須通過工作人員循環。 – dudemanbearpig

回答

0
echo $decode['workers']['username.1']['alive']; 

如果你想username.1,則:

foreach($decode['workers'] as $worker => $values) { 
    echo "username: $worker alive: {$values['alive']}"; 
} 
+0

謝謝你的工作! – user3112787