【问题标题】:Creating associative array from object从对象创建关联数组
【发布时间】:2019-01-17 15:33:29
【问题描述】:

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

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

如何修改它以创建我需要的关卡,以便在导出到电子表格时格式正确

【问题讨论】:

    标签: php arrays loops object associative


    【解决方案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
    }
    

    在没有看到实际数据的情况下编写此内容非常困难 - 所以我想您必须对其进行修改以适合您的数据。

    但我希望你明白这一点。祝你好运!

    【讨论】:

    • 我认为这是可行的,但我注意到的一件事是,每个 itemCode 的详细信息都有 2 组。那么我不需要围绕细节进行另一个 foreach 吗?
    • 是的 - 我没有意识到这一点。你需要另一个 for 循环 - 我会尽快更新我的帖子以支持它
    • 非常感谢
    • 是的,就是这样!再次感谢
    • 所以这会转储我想要的数据结构并导出到 xlsx,它会显示每个 category_name、category_desc 和 category_code。但是,我希望它将类别数据放在一行中,并将每个相关项目(itemCode 数据)作为下面的一行。这样不行吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多