我有一个看起来像这样的JSON文件......
{
"success": true,
"num_items": 6104,
"items": [
{
"market_name": "\u2605 Bayonet",
"market_hash_name": "\u2605 Bayonet",
"icon_url": "\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQh5hlcX0nvUOGsx8DdQBJjIAVHubSaKQZ53P3NZXMXvYmykdLSxqWkZ7-HkjMIvpIj3u2Y84733gzh_RU_MG_zIYLEdQ45fxiOrdJh0ExF",
"name_color": "8650AC",
"quality_color": "EB4B4B"
},
{
"market_name": "\u2605 Bayonet | Blue Steel (Battle-Scarred)",
"market_hash_name": "\u2605 Bayonet | Blue Steel (Battle-Scarred)",
"icon_url": "\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpotLu8JAllx8zJYAJA4MmzkL-DkvbiKvXTkzNVucNzj7mX9tWk21Xkr0JvN231JYGcdA47NF3Y81Hoxebs1sftot2XnmcyW1u0",
"name_color": "8650AC",
"quality_color": "EB4B4B"
},
// etc ...
]
我有一堆所谓的项目,我想通过只知道quality_color
获得market_hash_name
。我怎么能用PHP做到这一点?
答案 0 :(得分:3)
您需要做的是:
编辑:你很好地尝试了答案,所以这是我解决问题的有效方法。从你给我看的一小段代码中,问题是你不能使json_decode成为一个关联数组,而是将数组调用为一个数组。而不是json_decode($JSONString)
它应该是json_decode($JSONString, true)
<?php
$JSONin = '{
"success": true,
"num_items": 6104,
"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"
}
]
}';
$JSON = json_decode($JSONin, true);
$searched_market_hash_name = "★ Bayonet | Blue Steel (Battle-Scarred)";
foreach ($JSON['items'] as $item)
{
if ($searched_market_hash_name == $item['market_hash_name'])
{
echo "found! quality color is " . $item['quality_color'];
}
}