-1

I have a problem with fetching the correct data from a decoded JSON file. I don't know if my question is correct since I don't really know what I am doing for the moment.

So, this is what I don't want to do.

    $ln = 'https://api.steamprices.net/v2/csgoprices/?id='.market_hash_name.'&key=XXX';
    $link1 = file_get_contents($ln);
    $myarray1 = json_decode($link1, true);

   echo $myarray1['median_price'];

I am trying to get the price for every steam skin that's being loaded in my code. What this code does is that it loads this api link for every item I load. So if I have 50 items, this link will be loaded 50 times, which is not accepted by the API.

What I want to do, is that I want to load it once, and fetch the prices for every item from that exact link. That link would look like this:

https://api.steamprices.net/v2/csgoprices/?&key=XXX

So, lets say I load it once, and then when I want to apply market_hash_name to it, how do I do?

I assume it is something like this.

    $priceJson = file_get_contents('https://api.steamprices.net/v2/csgoprices/?key=XXX');
    $priceData = json_decode($priceJson, true);

    echo $priceData[''.$market_hash_name.'']['price'];

But it doesn't seem to work. I am sorry for this messy explanation, I an unfamiliar with this.

Note that an example response for the api link looks like this:

{
  "-r-H1Z1 Shirt": {
    "price": 0.11,
    "image": "https://steamcommunity-a.akamaihd.net/economy/image/iGm5OjgdO5r8OoJ7TJjS39tTyGCTzzQwmWl1QPRXu8oaf69-NOHLAbqw_23aLe8AcRQ8-3uyKA7_CGvsJYds9U65FMF7i6AbXTJ8PDm57EliZdK7KLPuuh3dxC3m4m0ihzss0MKE6NtIt4qs-JukOX73WgETXYze_pxEBA",
    "game": "h1z1"
  },
  "2016 Invitational Crate": {
    "price": 0.09,
    "image": "https://steamcommunity-a.akamaihd.net/economy/image/iGm5OjgdO5r8OoJ7TJjS39tTyGCTzzQwmWl1QPRXu8oaf69-NOHLAbqw_23aLe8AcRQ8-3uyKA7_CGvsJYds9U65FMF7i6APSjJ6BjX9rGBYZ9ioCPzysSX6hNNacA",
    "game": "h1z1"
  },
  "ANGRYPUG Motorcycle Helmet": {
    "price": 0.17,
    "image": "https://steamcommunity-a.akamaihd.net/economy/image/iGm5OjgdO5r8OoJ7TJjS39tTyGCTzzQwmWl1QPRXu8oaf69-NOHLAbqw_23aLe8AcRQ8-3uyKA7_CGvsJYds9U65FMF7i6AbXTJ8PDm57EliZdK7KLPuuh3WySnxyXoUgz870MKd7sFTkZq98oW1ORiqAVsCUYfbNu3SUQqvUSGyY__iEw",
    "game": "h1z1"
  },

Another output

{
    "name":"Aces High Pin",
    "price":1210,
    "have":2,
    "max":9,
    "rate":95,
    "tr":0
    }
PVP Squad
  • 11
  • 5
  • it's not clear what you mean with _"apply market_hash_name to it"_. Is '2016 Invitational Crate' a market_hash_name? – Jeff Oct 20 '18 at 18:11
  • Yes, that could be the market_hash_name. – PVP Squad Oct 20 '18 at 18:12
  • then it would be `echo $priceData['2016 Invitational Crate']['price'];` - is that what you want? – Jeff Oct 20 '18 at 18:12
  • Yes, and when I do it, I want to see 0.09 which is the price for 2016 Invitational Crate – PVP Squad Oct 20 '18 at 18:15
  • that's a bingo then!? – Jeff Oct 20 '18 at 18:15
  • Oh, sorry. No it doesn't display the price. I get this in return, `Notice: Undefined index: 2016 Invitational Crate in C:\xampp\htdocs\deposit.php on line 56` – PVP Squad Oct 20 '18 at 18:17
  • ah, you've changed it now. ok. those additional `''` are not needed, but should not be the problem. Can you do a `var_dump($priceData)` and show that? – Jeff Oct 20 '18 at 18:19
  • Yes, I recieved A LOT. So I'll just hand you this snippet of what it looks like for an item `["Dual Berettas | Black Limba (Well-Worn)"]=> array(3) { ["price"]=> float(0.66) ["image"]=> string(266)` – PVP Squad Oct 20 '18 at 18:23
  • and you are absolutely sure "2016 Invitational Crate" is in there aswell? Have you tried with "Dual Berettas | Black Limba (Well-Worn)"? – Jeff Oct 20 '18 at 18:27
  • Oh gosh, I wasn't sure. The other one worked. Thanks a lot dude! – PVP Squad Oct 20 '18 at 18:28
  • closing as a typo.... – Jeff Oct 20 '18 at 18:29
  • Please don't, how do I do if the output looks different? Like the example I added to the question now. – PVP Squad Oct 20 '18 at 18:33
  • here's everthing you need to know: https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php/29308899#29308899 – Jeff Oct 20 '18 at 19:03

1 Answers1

0

Well, the json string you provide isn't valid but something like this may help you

<?php

$jsonData=file_get_contents("json.file"); // simply contains your json string as posted
$jsonArray=json_decode($jsonData,true);
$jsonObject=json_decode($jsonData);

$list_of_MHN=array("2016 Invitational Crate","ANGRYPUG Motorcycle Helmet");

print_r($jsonArray);
exit;

foreach($jsonArray as $hash_name=>$arr){
    if(in_array($hash_name,$list_of_MHN)){
        print_r($arr);
    }
}

for($i=0;$i<count($list_of_MHN);$i++){
    if(isset($jsonArray[$list_of_MHN[$i]])){
        print_r($jsonArray[$list_of_MHN[$i]]);
    }
}


for($i=0;$i<count($list_of_MHN);$i++){
    if(isset($jsonObject->$list_of_MHN[$i])){
        print_r($jsonObject->$list_of_MHN[$i]);
    }
}

?>
ivanivan
  • 2,155
  • 2
  • 10
  • 11
  • Thanks, but the final answer was `$priceData['price'][''.$market_hash_name.'']` it was just a typo xD. However, look at the last json output example. How do I do if it's sorted like that? – PVP Squad Oct 20 '18 at 18:40
  • @PVPSquad dunno. Perhaps if the json you provided hadn't been invalid ... – ivanivan Oct 20 '18 at 18:46