seyroku seyroku - 2 years ago 76
JSON Question

Extract the value of a JSON with no keys on PHP

I'm trying to extract the value of a JSON, without a key that gives me a reference.

This is my JSON code:

{"StatTrak™ Dual Berettas | Dualing Dragons (Battle-Scarred)":0.37,"★ StatTrak™ Huntsman Knife | Scorched (Well-Worn)":101.65,"Sticker | iBUYPOWER | DreamHack 2014":11.34,"MP9 | Sand Dashed (Well-Worn)":0.03,"★ Flip Knife | Urban Masked (Field-Tested)":61.74}

The first value is the name and the second one the price. I've got a very long JSON with a lot of name's and prices.

name="StatTrak™ Dual Berettas | Dualing Dragons (Battle-Scarred)"<br>

Actually don't know how to access the name to extract the other value.

I have the name of the weapons saved on my site from another API. I have to extract the value of this JSON and compare that name with the name the API gave me previously.

Answer Source

The name is the key:

$array = json_decode($json, true);

foreach($array as $name => $price) {
    echo "$name<br>$price<br>";

You see it with a print_r($array):

    [StatTrakΓäó Dual Berettas | Dualing Dragons (Battle-Scarred)] => 0.37
    [Γÿà StatTrakΓäó Huntsman Knife | Scorched (Well-Worn)] => 101.65
    [Sticker | iBUYPOWER | DreamHack 2014] => 11.34
    [MP9 | Sand Dashed (Well-Worn)] => 0.03
    [Γÿà Flip Knife | Urban Masked (Field-Tested)] => 61.74
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download