0

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
        }
    }
13
  • Can you show output ? Commented Jan 16, 2019 at 20:40
  • $product->pricing is an object, not an array. Commented Jan 16, 2019 at 20:44
  • How do you go from json to $pricing->categories or can you show the actual value of $pricing->categories (preferably with var_export()) Commented Jan 16, 2019 at 20:45
  • You are just pushing everything in the array at the same level with $priceResult[] = something. What is your expected result structure? Commented Jan 16, 2019 at 20:46
  • @Barmar so I'm looping wrong at that level? Pricing and product_info are on the same level but I need info from each attached as one data set for the category Commented Jan 16, 2019 at 20:46

3 Answers 3

1

$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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I think this works but I have one question: Sometimes pricing has multiple things like "Main Price" : "120.00", "secondPrice" : "150.00". Is there a way with this object to do similar to what I did above and just tie Price : Amount
Things would be easier if you use the TRUE second argument to json_decode(). Then you'll get an associative array and you can loop over that.
This is laravel, so it decodes it by default. when I try to decode it, it treats it as already decoded and just gives me a null result
Is there a way to map it like that in the existing structure though?
You can use get_object_vars() to convert an object to an associative array. I've updated the answer.
1

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.

Comments

1

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...)

1 Comment

Would there be a way on price to map the key to the value rather than just using MainPrice? If there were 3 types and I didn't know what they were I would just want all 3 price amounts

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.