forep循环中的Php foreach没有显示正确的值

时间:2017-09-19 04:34:22

标签: php

我正在努力将值保存在csv文件中,这是我的数组的结构。所有信息都以正确的格式保存。但是,当我试图保存"属性字段"这是阵列到阵列然后它显示第二个记录相同的值,即使第二个阵列没有运输属性。如果我将foreach添加到主foreach之外然后它正在工作,但我需要将其运行到相同的循环所以我可以保存csv中的所有值。这是我的代码。

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

                )

        )


)

1 个答案:

答案 0 :(得分:0)

在开始内循环之前,您需要清除变量。

没有这些属性的项目正在使用上一次迭代中的项目。

Sub Convert()

    Rows(2).EntireRow.Hidden = True
    Rows(3).EntireRow.Hidden = True
    Rows(4).EntireRow.Hidden = True
    Rows(5).EntireRow.Hidden = True

    '##Below

    Dim sht As Worksheet
    Dim fndList As Variant
    Dim rplcList As Variant
    Dim x As Long

    fndList = Array(" CR 0.00 0.00 ", " DR 0.00 0.00 ", " 0.00 ", " Cr", " Dr", "0.00", "0.00 ")
    rplcList = Array(";-", ";", ";", "", "", "", "")
    For x = LBound(fndList) To UBound(fndList)
        For Each sht In ActiveWorkbook.Worksheets
            sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
                LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                SearchFormat:=False, ReplaceFormat:=False
        Next sht

        '##Above    
    Next x

    Range("A1") = "Account"
    Range("C1") = "Balance"
    Range("D1") = "KP"
    Range("A2", Range("A2").End(xlDown)).TextToColumns _
    Destination:=Range("A2"), DataType:=xlDelimited, Semicolon:=True
    Columns("B:C").AutoFit
    Range("A1", Range("A1").End(xlDown)).Copy
    Range("C1", Range("C1").End(xlToRight).End(xlDown)).PasteSpecial _
    Paste:=xlPasteFormats
    Application.CutCopyMode = False

    Columns(2).EntireColumn.Hidden = True

    Range("C:C").AutoFilter 1, "<>", , , False

End Sub