1

Here is my JSON file which is called (inventory.json). How do I parse the json so I can get all of the inventory's tag_name in the "Descriptions" array? Since the Inventory array's classid points out to the description's id/classid, it seems possible to get each of the inventory's tag from the description but I have no idea to do it.

I read something about recursive array iterator and for each but I have no idea which one is appropriate in this circumstance. I am very new here so please be kind! Thank you.

 {
    "Inventory": {
        "7905269096": {
            "id": "7905269096",
            "classid": "771158876",
            "instanceid": "782509058",
            "amount": "1",
            "pos": 1
        },
        "7832200468": {
            "id": "7832200468",
            "classid": "626495772",
            "instanceid": "1463199080",
            "amount": "1",
            "pos": 2
        },
        "7832199378": {
            "id": "7832199378",
            "classid": "626495770",
            "instanceid": "1463199082",
            "amount": "1",
            "pos": 3
        },
        "Descriptions": {
            "771158876": {
                "classid": "771158876",
                "instanceid": "782509058",
                "tags": [{
                    "tag_name": "unique",
                    "name": "standard"
                }]
            }
        }
    }
}
4
  • In your php file do $phparray = json_decode($json_string); Then var_dump($phparray);. This way you will (a) see the structure, and (b) be able to access the "tag_name" items. Commented Feb 12, 2016 at 14:34
  • 2
    Please add a valid JSON string to your question. This will increase the chance of people helping you. Commented Feb 12, 2016 at 14:34
  • Take a look at PHP's built in JSON methods, specifically json_decode php.net/manual/en/function.json-decode.php, that will convert the json into a php array in which then you'll be able to manipulate as normal within PHP. Commented Feb 12, 2016 at 14:35
  • hi I fixed my JSON, sorry for giving an invalid one! Commented Feb 12, 2016 at 14:39

1 Answer 1

2

Your JSON string is invalid, but hopefully this answer will lead you on the right path to your desired result.

First, make it into a PHP array:

$jsonArray = /* your json array */;
$phpArray = json_decode($jsonArray, true);

Then you can iterate through one of the arrays (the "Inventory" one) and find the relevant tag name:

//Create a new array to hold the key=>value data
$combined = [];

foreach($phpArray["Inventory"] as $i => $v){
    //Find the relevant key in the Descriptions array and
    //push this information information to the $combined array
    $combined[$i] = $phpArray["Descriptions"][$i]["tags"]["tag_name"];

    /* The above is essentially the same as
    $combined["7905269096"] = "unique"; */
}

Then, $combined will be a key/value array where the key is the ID (e.g. "7905269096") and the value will be the tag name (e.g. "unique").

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ben, I fixed my JSON file. Thank you for helping!
Are you sure that's fixed? Is Descriptions supposed to be inside of Inventory?
I think its separate arrays. but inventories point to the descriptions because I think, inventory ID is unique (Primary Key) and classid points to the description (foreign key relationship) since many ID can point to the class ID in the description. Thank you so much for helping me and trying to understand!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.