Geoff_S
Geoff_S

Reputation: 5107

Nested foreach loops on a json object not returning correct values and structure

I can't figure out why my array loop isn't working.

I'm trying to loop on a decoded JSON object

+"categoryCode": "1122"
+"category_description": "This is the category Description"
+"products": array:24 [▼
  0 => {#999 ▼
    +"pricing": {#1011 ▼
      +"MainPrice": "40.00"
    }
    +"productInfo": {#1009 ▼
      +"product": {#1014 ▼
        +"product_type": {#1015 ▼
          +"desc": "Test Product"
          +"quantDetails": 3.0
        }
      }
    }
  }

And build a new $priceResult array out of the values that I need. I want the category info on the first level of the array and then follow with product info.

WHy isn't this loop building my new array properly? When I dump $priceResult, I get the category info, but then I just get the price and a bunch of null values on the same level. Am I looping and building the new array incorrectly?

$priceResult = array();

foreach($pricing->categories as $category){ 

    $priceResult[] = $category->categoryCode;
    $priceResult[] = $category->category_description;

    foreach($category->products as $product){

        foreach ($product->pricing as $price => $amount) {

            $priceResult[] = $amount;

        }

        foreach($product->productInfo as $info){

            foreach($info->product as $product){

                foreach($product->product_type as $type){

                    $priceResult[] = $type->desc;
                    $priceResult[] = $type->quantDetails;
                }
            }
        }
    }
}

Update with output:

Here's an example of some erroneous output

      0 => "CategoryOne"
  1 => "1122"
  2 => "This is the category Description"
  3 => null
  4 => null
  5 => null
  6 => null
  7 => null
  8 => null
  9 => null
  10 => null
  11 => null
  12 => null
  13 => null
  14 => null
  15 => null
  16 => null
  17 => null
  18 => null
  19 => "40.00"
  20 => null
  21 => null
  22 => null
  23 => null
  24 => null
  25 => null
  26 => null
  27 => null
  28 => null
  29 => null
  30 => null
  31 => null
  32 => null
  33 => null
  34 => null
  35 => null
  36 => "50.00"

Update with desired output:

CategoryName : TestCategory
CategoryDescription: For Testing
    Products{
        0{
            Product_code : 123,
            CountNumber : 12,
            ProductDescription: Test Product,
            price_amount : 150.00
        },
        1{
            Product_code : 112,
            CountNumber : 32,
            ProductDescription: Test Product 2,
            price_amount : 250.00
        }
    }

Upvotes: 0

Views: 273

Answers (3)

Barmar
Barmar

Reputation: 780851

$product->pricing and $product->productInfo are single objects, not arrays of objects. If you want to loop over the properties of an object, you can use get_object_vars() to return an associative array.

foreach($pricing->categories as $category){ 

    $priceResult[] = $category->categoryCode;
    $priceResult[] = $category->category_description;

    foreach($category->products as $product){
        foreach (get_object_vars($product->pricing) as $amount) {
            $priceResult[] = $amount;
        }
        foreach (get_object_vars($product->productInfo) as $info) {
            $priceResult[] = $info->desc;
            $priceResult[] = $info->quantDetails;
        }
    }
}

Upvotes: 1

markmoxx
markmoxx

Reputation: 1532

Try this:

$categoryArray = [];

foreach($pricing->categories as $category) { 
    $categoryResult['categoryCode'] = $category->categoryCode;
    $categoryResult['CategoryDescription'] = $category->category_description;
    $categoryResult['Products'] = [];
    foreach($category->products as $product) {
        $productResult['Product_code'] = ''; // this doesn't appear in your JSON...
        $productResult['CountNumber'] = $product->productInfo->productType->quantDetails;
        $productResult['ProductDescription'] = $product->productInfo->productType->desc;
        $productResult['price_amount'] = $product->pricing->MainPrice;
        $categoryResult['Products'][] = $productResult;
    }
    $categoryArray[] = $categoryResult;
}

$priceResult = $categoryArray; // ($priceResult is a strange name for this array...)

Upvotes: 1

Aioros
Aioros

Reputation: 4383

I went with $priceResult being an array, containing category objects. I think it would look something like this:

$priceResult = array()

foreach($pricing->categories as $category){ 
    $c = new stdClass();
    $c->CategoryCode = $category->categoryCode;
    $c->CategoryDescription = $category->category_description;
    $c->Products = array();

    foreach($category->products as $product){
        $p = new stdClass();
        $p->ProductCode = null; // $product->something? no idea where this is
        $p->CountNumber = $product->productInfo->product->product_type->quantDetails;
        $p->ProductDescription = $product->productInfo->product->product_type->desc;
        $p->PriceAmount = $product->pricing->MainPrice;
        $c->Products[] = $p;
    }
    $priceResult[] = $c;
}

I do have to say, though, the original data seems to have a very weird structure.

Upvotes: 1

Related Questions