我正在努力将值保存在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] => Array
(
[Id] = 0046795ADQ36006102
[productGroupId] = 0046
[attributes] =Array
(
[0] => Array
(
[attributeId] => 116
[attributeName] => ShippingLength
[attributeValue] => 7 in
)
[1] => Array
(
[attributeId] => 117
[attributeName] => ShippingWidth
[attributeValue] => 1.75 in
)
[2] => Array
(
[attributeId] => 118
[attributeName] => ShippingHeight
[attributeValue] => 1.75 in
)
[3] => Array
(
[attributeId] => 119
[attributeName] => ShippingWeight
[attributeValue] => 1 lbs
)
)
)
)
Array
(
[0] => Array
(
[itemId] => 00201589083
[productGroupId] => 0020
[attributes] => Array
(
[0] => Array
(
[attributeId] => 152
[attributeName] => Part Type
[attributeValue] => 121572
)
)
)
[1] => Array
(
[itemId] => 00460469083
[productGroupId] => 0046
[attributes] => Array
(
[0] => Array
(
[attributeId] => 116
[attributeName] => ShippingLength
[attributeValue] => 13.9000 in
)
[1] => Array
(
[attributeId] => 117
[attributeName] => ShippingWidth
[attributeValue] => 2.5000 in
)
[2] => Array
(
[attributeId] => 118
[attributeName] => ShippingHeight
[attributeValue] => 2.5000 in
)
[3] => Array
(
[attributeId] => 119
[attributeName] => ShippingWeight
[attributeValue] => 0.7400 lbs
)
)
[restrictions] => Array
(
[0] => Array
(
[restrictionId] => 7
[restrictionTypeCd] => SHP
[restrictionDescription] => Only Normal Shipping Allowed
)
)
)
)
答案 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