从对象创建关联数组

时间:2019-01-17 15:33:30

标签: php arrays loops object associative

我目前有一个通过循环对象来使用正确数据构建的数组,但是给出的格式不正确:

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

如何修改它以创建所需的级别,以便在导出到电子表格时正确格式化

1 个答案:

答案 0 :(得分:1)

您需要拆分代码并在循环中初始化新对象。

请考虑以下内容(注意代码中的注释)

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

在写此信息时不会看到您的实际数据非常困难-因此我想您将不得不对其进行修改以适合您的数据。

但是我希望你能明白。祝你好运!

相关问题