Reputation: 13
I am stuck trying to figure out how I can have a foreach in a foreach without it being quantity $item x quantity $iteminfo. Have a look.
$inventory = file_get_contents("https://steamcommunity.com/profiles/".$_SESSION['steamid']."/inventory/json/730/2");
$myarrayInv = json_decode($inventory, true);
if(isset($myarrayInv['rgDescriptions']))
{
if(isset($myarrayInv['rgInventory'])) {
foreach($myarrayInv['rgDescriptions'] as $item) {
foreach($myarrayInv['rgInventory'] as $iteminfo) {
echo "<div class='item'><img class='item' src='https://steamcommunity-a.akamaihd.net/economy/image/".$item['icon_url']."' height='90' width='120'/><span class='caption'>".$item['name']." | ".$iteminfo['id']."</span></div> ";
}
}
}
This code is support to display items, when I remove the loop for "$iteminfo" the quantity of items displays right. Like this.
item1, item2, item3, item4.
When I add the forloop back, it gets like this.
item1, item1, item1, item1, item2, item2, item2, item2, item3, item3, item3, item3, item4 item4 item4 item4.
I don't know what a good solution would be, any ideas?
Edit: Maybe I was not clear enough about what I want to achieve, see. The $item array doens't contain the itemid, but $iteminfo does contain itemid.
I want the itemid to be displayed with the itemname and picture, this would work if I had a while-loop in a while-loop.
Upvotes: 0
Views: 957
Reputation: 4104
Each rgDescription is keyed by [classid_instanceid] from the rgInventory, not a straight like-index. So this may work out better for you (I ran a test using my personal steam inventory, seems to show icons and names):
$json = file_get_contents('https://steamcommunity.com/profiles/'. $_SESSION['steamid'] .'/inventory/json/730/2');
$data = json_decode($json, true);
foreach($data['rgInventory'] as $inv) {
$desc = $data['rgDescriptions'][ $inv['classid'] .'_'. $inv['instanceid'] ];
echo "<div class='item'><img class='item' src='https://steamcommunity-a.akamaihd.net/economy/image/". $desc['icon_url'] ."' height='90' width='120'/><span class='caption'>". $desc['name'] ." | ". $inv['id'] ."</span></div> ";
}
Upvotes: 1
Reputation: 780974
Just use one loop, and then use the same index in both arrays.
foreach ($myarrayInv['rgDescriptions'] as $index => $item) {
$iteminfo = $myarrayInv['rgInventory'][$index];
echo "<div class='item'><img class='item' src='https://steamcommunity-a.akamaihd.net/economy/image/".$item['icon_url']."' height='90' width='120'/><span class='caption'>".$item['name']." | ".$iteminfo['id']."</span></div> ";
}
You could also consider reorganizing your data. Instead of two arrays, use a single array with rgDescriptions
and rgInventory
keys.
Upvotes: 3