De Kirvis De Kirvis - 6 months ago 17
JSON Question

Reading from JSON, PHP

Example from JSON:

{
    "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"
        }
    ]
}


My PHP code. And how I tried to find 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;
}
}
}
}
}
}
}
}
}


It doesn't find correct value.
For example: I search for ★ M9 Bayonet | Slaughter (Field-Tested)
and it finds Bayonet | Slaughter (Field-Tested)

How I call this function?

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


Thanks

Answer

Try json_decode. You just need one foreach loop.

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