我已经成功构建了一个多维数组,可以将其转储:
0 => array:11 [▼
"category_code" => "123"
"category_name" => "Testing"
"category_description" => "This is a test category"
19738 => array:5 [▼
"identifier" => "720368842943"
"description" => Test Description One
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1129.00"
]
]
19739 => array:5 [▼
"identifier" => "720368844121"
"description" => "Test Description Two"
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1490.00"
]
]
但出于我的目的,我实际上需要一个2d数组,其中一个元素将是类别字段,然后每个项目及其所有信息将是另一个元素,以便可以将其导出为行。
如何使它成为二维数组?
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["category_code"] = $category->category_code;
$categoryItem["category_name"] = $category->category_name;
$categoryItem["category_desc"] = $category->category_desc;
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->sku_info->identifier;
$skuItem["description"] = $sku->sku_info->item->description;
$skuItem["count"] = $sku->sku_info->item->item_type->count;
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$detailsItem["detail_code"] = $details->detail_code;
$detailsItem["detail_code2"] = $details->detail_code2;
$detailsItem["detail_specifier"] = $details->detail_specifier;
$skuItem["details"][] = $detailsItem;
}
$skuItem["prices"] = get_object_vars($sku->prices);
$itemCode = $sku->sku_info->item->item_code;
$categoryItem[$itemCode] = $skuItem;
}
$allCategoryResult[] = $categoryItem;
}
预期输出示例
0 => array: [▼
"category_code" => "123"
"category_name" => "Testing"
"category_description" => "This is a test category"
Array:[
"item-code" => 19738
"identifier" => "720368842943"
"description" => Test Description One
"count" => 4
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
"01" => "1129.00"
]
]
答案 0 :(得分:1)
您需要做的就是不要将sku
的其他详细信息嵌套在单独的数组中。只需将其推入skuitems
数组即可,如下所示
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["category_code"] = $category->category_code;
$categoryItem["category_name"] = $category->category_name;
$categoryItem["category_desc"] = $category->category_desc;
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->sku_info->identifier;
$skuItem["description"] = $sku->sku_info->item->description;
$skuItem["count"] = $sku->sku_info->item->item_type->count;
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$skuItem["detail_code"] = $details->detail_code;
$skuItem["detail_code2"] = $details->detail_code2;
$skuItem["detail_specifier1"] = $details->detail_specifier1;
$skuItem["detail_specifier2"] = $details->detail_specifier2;
}
array_push($skuItem,$sku->prices);
$itemCode = $sku->sku_info->item->item_code;
$categoryItem[$itemCode] = $skuItem;
}
$allCategoryResult[] = $categoryItem;
}
P.S:数组键应该是唯一的,所以不能有两个名称相同的键“ detail_specifier”,因此在此处进行了相应的修改,但YMMV。