Foreach循环将最后一个结果添加到下一个

时间:2015-11-27 02:20:27

标签: php xml loops foreach

我一直在尝试使用SimplXML来解析一个非常大的XML文档中的一些特定信息,以创建一个新的简化文档。我正在使用产品,产品有变化(例如大小),我正在尝试构建到数组中。一些测试显示变体循环正在将最后的结果添加到下一个。例如:

第一个循环显示了这个,这对于具有2个变体的第一个产品是正确的。

["variations"]=>
array(2) {
  [0]=>
  array(1) {
    ["sku"]=>
    object(SimpleXMLElement)#19 (1) {
      [0]=>
      string(8) "00090011"
    }
  }
  [1]=>
  array(1) {
    ["sku"]=>
    object(SimpleXMLElement)#19 (1) {
      [0]=>
      string(8) "00090012"
    }
  }
}

现在循环中的第二个产品最终包含最后一个循环结果,并在它自己的结果之前添加它。

    ["variations"]=>
    array(4) {
      [0]=>
      array(1) {
        ["sku"]=>
        object(SimpleXMLElement)#19 (1) {
          [0]=>
          string(8) "00090011"
        }
      }
      [1]=>
      array(1) {
        ["sku"]=>
        object(SimpleXMLElement)#19 (1) {
          [0]=>
          string(8) "00090012"
        }
      }
      [3]=>
      array(1) {
        ["sku"]=>
        object(SimpleXMLElement)#19 (1) {
          [0]=>
          string(8) "00090013"
        }
      }
      [4]=>
      array(1) {
        ["sku"]=>
        object(SimpleXMLElement)#19 (1) {
          [0]=>
          string(8) "00090014"
        }
      }
    }

正如您所能想象的那样,最终会导致内存错误,并且最终会创建一个巨大的新XML文件,因为它预先设置了所有内容。这是我执行循环的PHP代码。

foreach($data as $element)
{

        if ($element->Dir_Name == "Fly Fishing"){
            //assign element nodes to variables
            $name = $element->PF_Name;
            $id = $element->PF_ID;
            $description = $element->PF_Description;
            $brand = $element->Manufacturer_Brand;
            $image = $element->ImageURL;
            $category1 = $element->Dir_Name;
            $category2 = $element->Group_Name;
            $category3 = $element->Cat_Name;

            //pull in variations
            $item_data = $element->Item;
            foreach($item_data->Sku as $variation){
                $optionName = $variation->Sku_Name;
                $price = $variation->Regular_Price;
                $sku =  $variation->Item_Code;

                $variation_array[] = array(
                    "optionName" => $optionName,
                    "price" => $price,
                    "sku" => $sku
                );
            }
            $products_array[] = array(
                "name" => $name,
                "description" => $description,
                "brand" => $brand,
                "image" => $image,
                "category1" => $category1,
                "category2" => $category2,
                "category3" => $category3,
                "variations" => $variation_array
            );
        }

}

我读过一些关于使用unset()来尝试纠正这个问题的内容,但它并没有成功。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要在$variation_array的每个循环上重置var $data,以避免在每个循环中构建它。

您可以将其重新声明为空数组,也可以取消设置var:

$variation_array = array();

或者

unset($variation_array);

完整代码:

foreach($data as $element)
{
    if ($element->Dir_Name == "Fly Fishing"){
        //assign element nodes to variables
        $name = $element->PF_Name;
        $id = $element->PF_ID;
        $description = $element->PF_Description;
        $brand = $element->Manufacturer_Brand;
        $image = $element->ImageURL;
        $category1 = $element->Dir_Name;
        $category2 = $element->Group_Name;
        $category3 = $element->Cat_Name;
        //pull in variations
        $item_data = $element->Item;
        $variation_array = array();
        foreach($item_data->Sku as $variation){
            $optionName = $variation->Sku_Name;
            $price = $variation->Regular_Price;
            $sku =  $variation->Item_Code;

            $variation_array[] = array(
                "optionName" => $optionName,
                "price" => $price,
                "sku" => $sku
            );
        }
        $products_array[] = array(
            "name" => $name,
            "description" => $description,
            "brand" => $brand,
            "image" => $image,
            "category1" => $category1,
            "category2" => $category2,
            "category3" => $category3,
            "variations" => $variation_array
        );
    }
}