2016-05-30 54 views
1
從JSON

實施例:雷丁從JSON,PHP

{ 
    "success": true, 
    "num_items": 6698, 
    "items": [ 
        { 
            "market_name": "★ Bayonet", 
            "market_hash_name": "★ Bayonet", 
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQh5hlcX0nvUOGsx8DdQBJjIAVHubSaKQZ53P3NZXMXvYmykdLSxqWkZ7-HkjMIvpIj3u2Y84733gzh_RU_MG_zIYLEdQ45fxiOrdJh0ExF", 
            "name_color": "8650AC", 
            "quality_color": "EB4B4B" 
        }, 
        { 
            "market_name": "★ Bayonet | Blue Steel (Battle-Scarred)", 
            "market_hash_name": "★ Bayonet | Blue Steel (Battle-Scarred)", 
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-DkvbiKvXTkzNVucNzj7mX9tWk21Xkr0JvN231JYGcdA47NF3Y81Hoxebs1sftot2XnmcyW1u0", 
            "name_color": "8650AC", 
            "quality_color": "EB4B4B" 
        }, 
        { 
            "market_name": "★ Bayonet | Blue Steel (Factory New)", 
            "market_hash_name": "★ Bayonet | Blue Steel (Factory New)", 
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-HnvD8J_WFxjIBvZIi3OqSo4j3jFbtr0NkY270cNLEJ1RsYV6DqAO8wum5gJ6_ot2Xnpnfakz6", 
            "name_color": "8650AC", 
            "quality_color": "EB4B4B" 
        }, 
        { 
            "market_name": "★ Bayonet | Blue Steel (Field-Tested)", 
            "market_hash_name": "★ Bayonet | Blue Steel (Field-Tested)", 
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-GkvP9JrafwzlUusN33bDAoI-ijg3sqRJra2DydoaXJAU5YgzUqALrxufmjJe5tIOJlyUq4M_FZA", 
            "name_color": "8650AC", 
            "quality_color": "EB4B4B" 
        }, 
        { 
            "market_name": "★ Bayonet | Blue Steel (Minimal Wear)", 
            "market_hash_name": "★ Bayonet | Blue Steel (Minimal Wear)", 
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-HnvD8J_WFxjIBvZIi3OqSo4j3jFbtr0NkY270cNLEJ1RsYV6DqAO8wum5gJ6_ot2Xnpnfakz6", 
            "name_color": "8650AC", 
            "quality_color": "EB4B4B" 
        }, 
        { 
            "market_name": "★ Bayonet | Blue Steel (Well-Worn)", 
            "market_hash_name": "★ Bayonet | Blue Steel (Well-Worn)", 
            "icon_url": "//steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-GkvP9JrafwzlUusN33bDAoI-ijg3sqRJra2DydoaXJAU5YgzUqALrxufmjJe5tIOJlyUq4M_FZA", 
            "name_color": "8650AC", 
            "quality_color": "EB4B4B" 
        } 
    ] 
} 

我的PHP代碼。我怎麼試圖找到icon_url:

function getSkinUrl($json, $skinName) 
{ 
    foreach($json as $key => $value) 
    { 
     if ($key == "items") 
     { 
      foreach($value as $key => $value1) 
      { 
       foreach($value1 as $key => $value) 
       { 
        if ($key == "market_name") // && $value == $skinName 
        { 
         if ($value == $skinName) 
         { 

          foreach($value1 as $key => $value) 
          { 
           if ($key == "icon_url") 
           { 
            $str2 = substr($value, 2); 
            return "http://" . $str2; 
            break; 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

它找不到正確的值。 例如:我搜索★M9刺刀|屠宰(實地測試) 它發現刺刀|屠宰(現場測試)

我怎麼稱呼這個功能?

$jsondata = file_get_contents($path."/730.json"); 
ini_set('memory_limit', '-1'); 
$json = json_decode($jsondata, true); 
getSkinUrl($json,"skin name"); 

感謝

+1

您經常使用'$ value'和'$ value1'。在第9行中,在循環中使用'$ value',破壞外部循環7中的'$ value'。 – Holger

回答

0

嘗試json_decode。你只需要一個foreach循環。

$array = json_decode($jsondata, true); 
foreach ($array['items'] as $item) { 
    if ($item['market_name'] == ...) { 
     return $item['icon_url']; 
    } 
} 
0

你應該json_decode看看:

$jsonObject = json_decode($json); 

foreach ($jsonObject->items as $item) 
    if ($item->market_name == ... 
etc.