Reputation: 33
I have a JSON file that looks like this...
{
"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 ...
]
I have a bunch of so called items, and I want to get the quality_color
by ONLY knowing the market_hash_name
. How can I do this using PHP?
Upvotes: 2
Views: 120
Reputation: 1484
What you will need to do is the following:
EDIT: You made a good attempt at the answer, so here's my working solution to your problem. From the small snippet of code you have shown me the problem is that you aren't making the json_decode an associative array but are calling the array as if it was one. Instead of json_decode($JSONString)
it should be 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'];
}
}
Upvotes: 3