Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I have an piece of JSON data. Remember: I am showing piece!

{
  "status" : "success",
  "prices" : [
    {
      "market_hash_name" : "AK-47 | Aquamarine Revenge (Battle-Scarred)",
      "price" : "15",
      "created_at" : 1460708937
    },

Here's my PHP, which should get price, but it doesn't work. What could be the issue? 'price'=>$prices->prices[0]->market_hash_name[$row['market_hash_name']]->price);

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to iterate over your prices to find the matching market_hash_name using $row['market_hash_name']. Then you can extract the price from that matching object.

I tried to keep your code as close to the same as possible.

$sql = $db->query('SELECT * FROM `items`');
$prices2 = file_get_contents('cache/SuiPrices.json');
$prices2 = json_decode($prices);
while ($row = $sql->fetch()) {
    $price = null;
    foreach($prices2->prices as $data) {
        if($row['market_hash_name'] == $data->market_hash_name) {
            $price = (float) $data->price;

            break;
        }
    }

    $array['items'][] = array(
        'img' => 'http://steamcommunity-a.akamaihd.net/economy/image/'.$row['img'].'/200fx200',
         'name' => $row['market_hash_name'],
         'assetid' => $row['assetid'],
         'price' => $price
    }
}

exit(json_encode($array));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...