json对象上的嵌套foreach循环未返回正确的值和结构

时间:2019-01-16 20:38:25

标签: php arrays json foreach

我不知道为什么我的数组循环不起作用。

我正在尝试在解码的JSON对象上循环

+"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
        }
      }
    }
  }

然后根据我需要的值构建一个新的$priceResult数组。我想要数组第一层的类别信息,然后再输入产品信息。

为什么这个循环不能正确地构建我的新数组?当我转储$ priceResult时,我得到了类别信息,但随后我只获得了价格和同一级别上的一堆空值。我循环并错误地构建新阵列吗?

$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;
                }
            }
        }
    }
}

更新输出:

这是一些错误输出的例子

      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"

使用所需的输出进行更新:

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
        }
    }

3 个答案:

答案 0 :(得分:1)

$product->pricing$product->productInfo是单个对象,而不是对象数组。如果要遍历对象的属性,可以使用get_object_vars()返回一个关联数组。

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;
        }
    }
}

答案 1 :(得分:1)

我将$priceResult作为一个包含类别对象的数组。我认为它看起来像这样:

$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;
}

我不得不说,原始数据的结构似乎很奇怪。

答案 2 :(得分:1)

尝试一下:

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