1

I currently have a decoded JSON array

+"productINF": {#1260 ▼
  +"product": {#1011 ▼
    +"productCode": "123"
    +"productType": {#999 ▼
      +"count": 3.0
      +"desc": "Block"
    }
  }
}
+"price": {#1267 ▼
  +"02": "470.00"
}

And I'm performing multiple foreach loops to get info from each level that I need. The problem is that I have two things on the same level, each their own array: productINF and price.

The first has info on the product and the second has price info. The issue is that in the current JSON array the price is "02" : "470.00" but sometimes there may be multiple prices like so:

+"01": "40.00",
+"05": "240.00"

I don't ever know what the key is going to be but I just want to make sure that foreach item, I call the 2nd value as the price. Here's how I'm looping now, but I don't know how to set the price to a value correctly:

 foreach($category->skus as $sku){
      foreach($sku->productINF as $info){
          $productCode = $info->productCode;

          foreach($info->productType as $type){
              $count = $type->count;
              $desc = $type->desc;
          }
      }
      foreach ($sku->price as $price) {
         //Not sure how to access price here
      }
  }
1
  • When there are multiple prices you want all of them in an array or what? Commented Jan 15, 2019 at 16:17

1 Answer 1

1

A foreach will treat objects the same as arrays.

This should do...

foreach ($sku->price as $price) {
    foreach ($price as $id => $amount) {
        echo $id , ' ' , $amount , "\n";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.