【问题标题】:Nested foreach loops on a json object not returning correct values and structurejson对象上的嵌套foreach循环未返回正确的值和结构
【发布时间】:2019-06-11 00:11:58
【问题描述】:

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

我正在尝试循环解码的 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
        }
    }

【问题讨论】:

  • 你能显示输出吗?
  • $product->pricing 是一个对象,而不是一个数组。
  • 如何从json$pricing->categories 或者你能显示$pricing->categories 的实际值(最好是var_export()
  • 您只是将数组中的所有内容推送到与$priceResult[] = something 相同的级别。您预期的结果结构是什么?
  • @Barmar 所以我在那个级别上循环错误?定价和 product_info 处于同一水平,但我需要每个附加的信息作为该类别的一个数据集

标签: php arrays json foreach


【解决方案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;
        }
    }
}

【讨论】:

  • 谢谢,我认为这行得通,但我有一个问题:有时定价有多种内容,例如"Main Price" : "120.00", "secondPrice" : "150.00"。有没有办法让这个对象做类似于我上面所做的事情,只是绑定价格:数量
  • 如果您将TRUE 第二个参数用于json_decode(),事情会更容易。然后你会得到一个关联数组,你可以循环它。
  • 这是 laravel,所以默认解码。当我尝试对其进行解码时,它会将其视为已解码并只给我一个空结果
  • 有没有办法像这样在现有结构中映射它?
  • 您可以使用get_object_vars() 将对象转换为关联数组。我已经更新了答案。
【解决方案2】:

我选择了 $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;
}

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

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

    • 是否有一种方法可以在价格上将键映射到值,而不仅仅是使用 MainPrice?如果有 3 种类型并且我不知道它们是什么,我只想要所有 3 种价格金额
    猜你喜欢
    • 2021-11-11
    • 2013-05-27
    • 2019-05-22
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2015-04-12
    相关资源
    最近更新 更多