2017-09-19 171 views
1

我正在將值保存在csv文件中,這是我的數組結構。所有信息都以正確的格式保存。但是當我試圖保存「屬性字段」的數組到數組中的值,那麼它顯示第二條記錄相同的值,即使第二個數組沒有運輸屬性。如果我將foreach添加到主foreach之外,那麼它是工作,但我需要運行這個到相同的循環,這樣我可以保存所有的值csv。這是我的代碼。在foreach循環中的PHP foreach沒有顯示正確的值

$output = fopen('php://output', 'w'); 
header('Content-Type: text/csv; charset=utf-8'); 
header('Content-Disposition: attachment; filename=searsparts.csv'); 
header('Pragma: no-cache'); 
fputcsv($output, array('id','groupid','length','width','height','weight')); 
foreach($partsDetail['item'] as $v=>$valPartsDetail) 
{ 
    $itemId = trim($valPartsDetail['Id']); 
    $productGrpName = trim($valPartsDetail['productGroupId']); 
    foreach ($valPartsDetail['attributes'] as $key => $attrVal) 
    { 
     $explodeAttr = explode(' ', $attrVal['attributeValue']); 
     if ($attrVal['attributeName'] == "ShippingLength") { 
      $length = $explodeAttr[0]; 
     } 
     if ($attrVal['attributeName'] == "ShippingWidth") { 
      $width = $explodeAttr[0]; 
     } 
     if ($attrVal['attributeName'] == "ShippingHeight") { 
      $height = $explodeAttr[0]; 
     } 
     if ($attrVal['attributeName'] == "ShippingWeight") { 
      $weight = $explodeAttr[0]; 
     } 

    } 
    $partsData = array($id, $groupId, $length, $height, $width, $weight); 
    fputcsv($output, $partsData); 
    echo "length==".$length."==width==".$width."==height==".$height."==weight".$weight."<br/>"; 

} 
exit; 

Array 
(
    [0] =&gt; Array 
     (
      [Id] = 0046795ADQ36006102 
      [productGroupId] = 0046 
      [attributes] =Array 
       (
        [0] =&gt; Array 
         (
          [attributeId] =&gt; 116 
          [attributeName] =&gt; ShippingLength 
          [attributeValue] =&gt; 7 in 
         ) 

        [1] =&gt; Array 
         (
          [attributeId] =&gt; 117 
          [attributeName] =&gt; ShippingWidth 
          [attributeValue] =&gt; 1.75 in 
         ) 

        [2] =&gt; Array 
         (
          [attributeId] =&gt; 118 
          [attributeName] =&gt; ShippingHeight 
          [attributeValue] =&gt; 1.75 in 
         ) 

        [3] =&gt; Array 
         (
          [attributeId] =&gt; 119 
          [attributeName] =&gt; ShippingWeight 
          [attributeValue] =&gt; 1 lbs 
         ) 
       ) 

     ) 

) 
Array 
(
    [0] =&gt; Array 
     (
      [itemId] =&gt; 00201589083 
      [productGroupId] =&gt; 0020 
      [attributes] =&gt; Array 
       (
        [0] =&gt; Array 
         (
          [attributeId] =&gt; 152 
          [attributeName] =&gt; Part Type 
          [attributeValue] =&gt; 121572 
         ) 

       ) 

     ) 

    [1] =&gt; Array 
     (
      [itemId] =&gt; 00460469083 
      [productGroupId] =&gt; 0046 
      [attributes] =&gt; Array 
       (
        [0] =&gt; Array 
         (
          [attributeId] =&gt; 116 
          [attributeName] =&gt; ShippingLength 
          [attributeValue] =&gt; 13.9000 in 
         ) 

        [1] =&gt; Array 
         (
          [attributeId] =&gt; 117 
          [attributeName] =&gt; ShippingWidth 
          [attributeValue] =&gt; 2.5000 in 
         ) 

        [2] =&gt; Array 
         (
          [attributeId] =&gt; 118 
          [attributeName] =&gt; ShippingHeight 
          [attributeValue] =&gt; 2.5000 in 
         ) 

        [3] =&gt; Array 
         (
          [attributeId] =&gt; 119 
          [attributeName] =&gt; ShippingWeight 
          [attributeValue] =&gt; 0.7400 lbs 
         ) 


       ) 
      [restrictions] =&gt; Array 
       (
        [0] =&gt; Array 
         (
          [restrictionId] =&gt; 7 
          [restrictionTypeCd] =&gt; SHP 
          [restrictionDescription] =&gt; Only Normal Shipping Allowed 
         ) 

       ) 

     ) 


) 
+0

如何在'foreach()'裏面獲得'$ id,$ groupId'? –

回答

0

在開始內循環之前,您需要清除變量。

沒有這些屬性的項目正在使用上一次迭代中的項目。

$itemId = trim($valPartsDetail['Id']); 
$productGrpName = trim($valPartsDetail['productGroupId']); 
$length = null; 
$width = null; 
$height = null; 
$weight = null; 
foreach ($valPartsDetail['attributes'] as $key => $attrVal) 
{ 
    ... 
+0

它不會工作,因爲在每次循環迭代本身之後,它們本身都會消失。 –

+0

不,他們沒有,他們在這個例子的全球範圍內。 – Scuzzy

+1

非常感謝它的工作 – testingexpert