我正在將值保存在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] => 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
)
)
)
)
如何在'foreach()'裏面獲得'$ id,$ groupId'? –