从对象创建关联数组
Creating associative array from object
我目前有一个数组,它通过循环对象使用正确的数据构建,但它提供的格式不正确:
$priceResult = array();
foreach($prices->categories as $category){
$priceResult[] = $category->category_name;
$priceResult[] = $category->category_desc;
$priceResult[] = $category->category_code;
foreach($category->products as $product){
$priceResult[] = $product->product_info->item->item_code;
foreach ($product->product_info->details as $details) {
$priceResult[] = $details->description;
$priceResult[] = $details->color;
$priceResult[] = $details->sector;
}
$priceResult[] = $product->product_info->code;
$priceResult[] = $product->product_info->item->description;
$priceResult[] = $product->product_info->item->item_type->quantity;
foreach(get_object_vars($product->prices) as $amount){
$priceResult[] = $amount;
}
}
}
虽然这不是关联的。
所以目前,假设我有一个类别有两种产品,然后它们都作为一个数组打印出来
array({
1:category_name
2:category_desc
3:category_code
4:item_code
5:description
6:color
7:sector
8:code
9:description
10:quantity
11:amount
12:item_code
13:description
14:color
15:sector
16:code
17:description
18:quantity
19:amount
})
我想要一个结构,其中父级别是 category_code 及其名称和描述,然后是每个 item_code 和它们自己的信息,如下所示:
array({
category_name
category_desc
category_code
Array(
1: item_code array(
details array(
description
color
sector
)
code
description
quantity
amount)
2: item_code array(
details array(
description
color
sector
)
code
description
quantity
amount)
)
})
我如何修改它来创建我需要的级别,以便在我导出到电子表格时它的格式正确
您需要拆分代码并在循环中初始化新对象。
考虑以下内容(注意代码中的注释)
$allCategoryResult= array(); // init at first - notice naming as category and not prices
foreach($prices->categories as $category){
$categoryItem = array(); // as current category to populate
// give name to the keys and not just numbers
$categoryItem["name"] = $category->category_name;
$categoryItem["desc"] = $category->category_desc;
$categoryItem["code"] = $category->category_code;
foreach($category->products as $product){
$productItem = array(); // new product, so init new array for him
// fill all the item data with name - maybe you will need to fix the paths here
$productItem["details"] = array(); // init empty array for all the details elements
foreach ($product->product_info->details as $details) {
$detailsItem = array(); // init details array for each detail element
$detailsItem["description"] = $details->description;
$detailsItem["color"] = $details->color;
$detailsItem["sector"] = $details->sector;
$productItem["details"][] = $detailsItem; // add the detail element to the product array
}
$productItem["code"] = $product->product_info->code;
$productItem["itemDescription"] = $product->product_info->item->description;
$productItem["quantity"] = $product->product_info->item->item_type->quantity;
$productItem["amount"] = get_object_vars($product->prices);
$itemCode = $product->product_info->item->item_code;
categoryItem[$itemCode] = $productItem; // add the product to category array by his code
}
$allCategoryResult[] = $categoryItem; //add the category to all category array
}
在没有看到实际数据的情况下编写此代码非常困难 - 所以我想您将不得不修改它以适合您的数据。
但我希望你明白了。祝你好运!
我目前有一个数组,它通过循环对象使用正确的数据构建,但它提供的格式不正确:
$priceResult = array();
foreach($prices->categories as $category){
$priceResult[] = $category->category_name;
$priceResult[] = $category->category_desc;
$priceResult[] = $category->category_code;
foreach($category->products as $product){
$priceResult[] = $product->product_info->item->item_code;
foreach ($product->product_info->details as $details) {
$priceResult[] = $details->description;
$priceResult[] = $details->color;
$priceResult[] = $details->sector;
}
$priceResult[] = $product->product_info->code;
$priceResult[] = $product->product_info->item->description;
$priceResult[] = $product->product_info->item->item_type->quantity;
foreach(get_object_vars($product->prices) as $amount){
$priceResult[] = $amount;
}
}
}
虽然这不是关联的。
所以目前,假设我有一个类别有两种产品,然后它们都作为一个数组打印出来
array({
1:category_name
2:category_desc
3:category_code
4:item_code
5:description
6:color
7:sector
8:code
9:description
10:quantity
11:amount
12:item_code
13:description
14:color
15:sector
16:code
17:description
18:quantity
19:amount
})
我想要一个结构,其中父级别是 category_code 及其名称和描述,然后是每个 item_code 和它们自己的信息,如下所示:
array({
category_name
category_desc
category_code
Array(
1: item_code array(
details array(
description
color
sector
)
code
description
quantity
amount)
2: item_code array(
details array(
description
color
sector
)
code
description
quantity
amount)
)
})
我如何修改它来创建我需要的级别,以便在我导出到电子表格时它的格式正确
您需要拆分代码并在循环中初始化新对象。
考虑以下内容(注意代码中的注释)
$allCategoryResult= array(); // init at first - notice naming as category and not prices
foreach($prices->categories as $category){
$categoryItem = array(); // as current category to populate
// give name to the keys and not just numbers
$categoryItem["name"] = $category->category_name;
$categoryItem["desc"] = $category->category_desc;
$categoryItem["code"] = $category->category_code;
foreach($category->products as $product){
$productItem = array(); // new product, so init new array for him
// fill all the item data with name - maybe you will need to fix the paths here
$productItem["details"] = array(); // init empty array for all the details elements
foreach ($product->product_info->details as $details) {
$detailsItem = array(); // init details array for each detail element
$detailsItem["description"] = $details->description;
$detailsItem["color"] = $details->color;
$detailsItem["sector"] = $details->sector;
$productItem["details"][] = $detailsItem; // add the detail element to the product array
}
$productItem["code"] = $product->product_info->code;
$productItem["itemDescription"] = $product->product_info->item->description;
$productItem["quantity"] = $product->product_info->item->item_type->quantity;
$productItem["amount"] = get_object_vars($product->prices);
$itemCode = $product->product_info->item->item_code;
categoryItem[$itemCode] = $productItem; // add the product to category array by his code
}
$allCategoryResult[] = $categoryItem; //add the category to all category array
}
在没有看到实际数据的情况下编写此代码非常困难 - 所以我想您将不得不修改它以适合您的数据。
但我希望你明白了。祝你好运!