访问嵌套的foreach PHP中的值

I have a JSON file. (Steam API). I want to display player inventory in my website. I use this url: https://steamcommunity.com/id/majidsajadi/inventory/json/730/2

Then I decode it with json_decode function.

Now there are two arrays: rgInventory and rgDescription. I need to check if class id in rgInventory and classid in rgDescription match I use some of values in igDescription.

So I think I should use 2 foreach loop and a if condition to check if class id match. then I echo out the information I need.

The question is how should I use nested foreach?

You can use two nested foreach loops like this:

Assuming that $decoded_data is your decoded data after applying json_decode() function on the end result, like this: $decoded_data = json_decode($your_json_data, true);

foreach($decoded_data['rgInventory'] as $arr1){
    foreach($decoded_data['rgDescriptions'] as $arr2){
        if($arr1['classid'] == $arr2['classid']){

            // both the classid matches
            // your code

        }
    }
}

Try this code:

$steamData = json_decode(file_get_contents("https://steamcommunity.com/id/majidsajadi/inventory/json/730/2"), true);

if($steamData["success"] != 1){
    exit(); 
}

$match = array();

foreach($steamData["rgInventory"] as $v1){
    $match[$v1["classid"]]["match"] = false;
    $match[$v1["classid"]]["count"] = 0;
}

foreach($steamData["rgDescriptions"] as $v2){
    if(array_key_exists($v2["classid"],$match)){
        $match[$v2["classid"]]["match"] = true;
        $match[$v2["classid"]]["count"]++;
    }
}

print_r($match);