按列中的列对多维数组中的数据进行分组,并在每个组内创建子数组

问题描述 投票:0回答:3

我需要按

['DispensaryInventory']['product_id']
(或
['Product']['id']
)值对多维数组中的数据集进行分组,并将分组数据推送到
DispensaryInventory
ProductOption
的子数组中。

我的阵列:

$dispensary_inventory_data = [
    [
        'DispensaryInventory' => [
            'id' => 15,
            'dispensary_id' => 1,
            'product_id' => 9,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => 2, 'unit' => 'oz'],
        'Product' => ['id' => 9, 'name' => 'Bajaj']
    ],
    [
        'DispensaryInventory' => [
            'id' => 11,
            'dispensary_id' => 1,
            'product_id' => 9,
            'quantity' => 17,
            'price' => 12.00
        ],
        'ProductOption' => ['value' => '1/8', 'unit' => 'oz'],
        'Product' => ['id' => 9, 'name' => 'Bajaj']
    ],
    [
        'DispensaryInventory' => [
            'id' => 12,
            'dispensary_id' => 1,
            'product_id' => 9,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => '1/4', 'unit' => 'oz'],
        'Product' => ['id' => 9, 'name' => 'Bajaj']
    ],
    [
        'DispensaryInventory' => [
            'id' => 13,
            'dispensary_id' => 1,
            'product_id' => 9,
            'quantity' => 5,
            'price' => 123.00
        ],
        'ProductOption' => ['value' => '1/2', 'unit' => 'oz'],
        'Product' => ['id' => 9, 'name' => 'Bajaj']
    ],
    [
        'DispensaryInventory' => [
            'id' => 14,
            'dispensary_id' => 1,
            'product_id' => 9,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => 1, 'unit' => 'oz'],
        'Product' => ['id' => 9, 'name' => 'Bajaj']
    ],
    [
        'DispensaryInventory' => [
            'id' => 5,
            'dispensary_id' => 1,
            'product_id' => 8,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => 2, 'unit' => 'oz'],
        'Product' => ['id' => 8, 'name' => 'BMW']
    ],
    [
        'DispensaryInventory' => [
            'id' => 1,
            'dispensary_id' => 1,
            'product_id' => 8,
            'quantity' => 2,
            'price' => 123.00
        ],
        'ProductOption' => ['value' => '1/8', 'unit' => 'oz'],
        'Product' => ['id' => 8, 'name' => 'BMW']
    ],
    [
        'DispensaryInventory' => [
            'id' => 2,
            'dispensary_id' => 1,
            'product_id' => 8,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => '1/4', 'unit' => 'oz'],
        'Product' => ['id' => 8, 'name' => 'BMW']
    ],
    [
        'DispensaryInventory' => [
            'id' => 3,
            'dispensary_id' => 1,
            'product_id' => 8,
            'quantity' => 23,
            'price' => 222.00
        ],
        'ProductOption' => ['value' => '1/2', 'unit' => 'oz'],
        'Product' => ['id' => 8, 'name' => 'BMW']
    ],
    [
        'DispensaryInventory' => [
            'id' => 4,
            'dispensary_id' => 1,
            'product_id' => 8,
            'quantity' => 12,
            'price' => 232.00
        ],
        'ProductOption' => ['value' => 1, 'unit' => 'oz'],
        'Product' => ['id' => 8, 'name' => 'BMW']
    ],
    [
        'DispensaryInventory' => [
            'id' => 6,
            'dispensary_id' => 1,
            'product_id' => 3,
            'quantity' => 3,
            'price' => 21.00
        ],
        'ProductOption' => ['value' => '1/8', 'unit' => 'oz'],
        'Product' => ['id' => 3, 'name' => 'Yash product']
    ],
    [
        'DispensaryInventory' => [
            'id' => 7,
            'dispensary_id' => 1,
            'product_id' => 3,
            'quantity' => 12,
            'price' => 56.00
        ],
        'ProductOption' => ['value' => '1/4', 'unit' => 'oz'],
        'Product' => ['id' => 3, 'name' => 'Yash product']
    ],
    [
        'DispensaryInventory' => [
            'id' => 8,
            'dispensary_id' => 1,
            'product_id' => 3,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => '1/2', 'unit' => 'oz'],
        'Product' => ['id' => 3, 'name' => 'Yash product']
    ],
    [
        'DispensaryInventory' => [
            'id' => 9,
            'dispensary_id' => 1,
            'product_id' => 3,
            'quantity' => 15,
            'price' => 354.00
        ],
        'ProductOption' => ['value' => 1, 'unit' => 'oz'],
        'Product' => ['id' => 3, 'name' => 'Yash product']
    ],
    [
        'DispensaryInventory' => [
            'id' => 10,
            'dispensary_id' => 1,
            'product_id' => 3,
            'quantity' => '',
            'price' => ''
        ],
        'ProductOption' => ['value' => 2, 'unit' => 'oz'],
        'Product' => ['id' => 3, 'name' => 'Yash product']
    ]
];

我希望它看起来像这个数组(包含 3、8 和 9 组):

Array
(
    [0] => Array
        (
            [DispensaryInventory] => Array
                (
                  [0] => Array
                       (
                            [id] => 15
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 
                            [price] => 
                       )
                  [1] => Array
                       (
                            [id] => 11
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 17
                            [price] => 12.00
                       )
                  [2] => Array
                       (
                             [id] => 12
                             [dispensary_id] => 1
                             [product_id] => 9
                             [quantity] => 
                             [price] =>
                       )
                  [3] => Array
                       (
                            [id] => 13
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 5
                            [price] => 123.00 
                       )
                  [4] => Array
                       (
                            [id] => 14
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 
                            [price] => 
                       )
                       
                )

            [ProductOption] => Array
                (
                   [0] => Array
                       (
                            [value] => 2
                            [unit] => oz
                       )
                   [1] => Array
                       (
                            [value] => 1/8
                            [unit] => oz
                       )
                   [2] => Array
                       (
                            [value] => 1/4
                            [unit] => oz
                       )
                   [3] => Array
                       (
                            [value] => 1/2
                            [unit] => oz
                       )
                   [4] => Array
                       (
                            [value] => 1
                            [unit] => oz
                       )
                       
                )

            [Product] => Array
                (
                    [id] => 9
                    [name] => Bajaj
                )

        )

    [1] => Array
        (
            [DispensaryInventory] => Array
                (
                  [0] => Array
                       (
                            [id] => 5
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 
                            [price] =>  
                       )
                  [1] => Array
                       (
                            [id] => 1
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 2
                            [price] => 123.00
                       )
                  [2] => Array
                       (
                             [id] => 2
                             [dispensary_id] => 1
                             [product_id] => 8
                             [quantity] => 
                             [price] => 
                       )
                  [3] => Array
                       (
                            [id] => 3
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 23
                            [price] => 222.00
                       )
                  [4] => Array
                       (
                            [id] => 4
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 12
                            [price] => 232.00
                       )
                       
                )

            [ProductOption] => Array
                (
                   [0] => Array
                       (
                            [value] => 2
                            [unit] => oz
                       )
                   [1] => Array
                       (
                            [value] => 1/8
                            [unit] => oz
                       )
                   [2] => Array
                       (
                            [value] => 1/4
                            [unit] => oz
                       )
                   [3] => Array
                       (
                            [value] => 1/2
                            [unit] => oz
                       )
                   [4] => Array
                       (
                            [value] => 1
                            [unit] => oz
                       )
                       
                )

            [Product] => Array
                (
                    [id] => 8
                    [name] => BMW
                )

        )    

    [2] => Array
        (
            [DispensaryInventory] => Array
                (
                  [0] => Array
                       (
                             [id] => 6
                             [dispensary_id] => 1
                             [product_id] => 3
                             [quantity] => 3
                             [price] => 21.00  
                       )
                  [1] => Array
                       (
                            [id] => 7
                            [dispensary_id] => 1
                            [product_id] => 3
                            [quantity] => 12
                            [price] => 56.00
                       )
                  [2] => Array
                       (
                             [id] => 2
                             [id] => 8
                             [dispensary_id] => 1
                             [product_id] => 3
                             [quantity] => 
                             [price] =>  
                       )
                  [3] => Array
                       (
                            [id] => 9
                            [dispensary_id] => 1
                            [product_id] => 3
                            [quantity] => 15
                            [price] => 354.00
                       )
                  [4] => Array
                       (
                            [id] => 10
                            [dispensary_id] => 1
                            [product_id] => 3
                            [quantity] => 
                            [price] =>
                       )
                       
                )

            [ProductOption] => Array
                (
                   [0] => Array
                       (
                            [value] => 2
                            [unit] => oz
                       )
                   [1] => Array
                       (
                            [value] => 1/8
                            [unit] => oz
                       )
                   [2] => Array
                       (
                            [value] => 1/4
                            [unit] => oz
                       )
                   [3] => Array
                       (
                            [value] => 1/2
                            [unit] => oz
                       )
                   [4] => Array
                       (
                            [value] => 1
                            [unit] => oz
                       )
                       
                )

            [Product] => Array
                (
                    [id] => 3
                    [name] => Yash product
                )

        )        

我的编码尝试:

foreach ($dispensary_inventory_data as $k1 => $a1) {
    foreach ($dispensary_inventory_data as $k2 => $a2) {
        if ($k1 < $k2 && $a1["DispensaryInventory"]["product_id"] == $a2["DispensaryInventory"]["product_id"]) {
            $dispensary_inventory_data[$k1]["ProductOption"][] = $a2["ProductOption"];
            $dispensary_inventory_data[$k1]["Product"][] = $a2["Product"];
            if (isset($dispensary_inventory_data[$k1]["Product"]["id"])) {
                $dispensary_inventory_data[$k1]["Product"][] = array(
                    "id" => $dispensary_inventory_data[$k1]["Product"]["id"],
                    "name" => $dispensary_inventory_data[$k1]["Product"]["name"],
                    "quantity" => $dispensary_inventory_data[$k1]["Product"]["quantity"]
                );
                $dispensary_inventory_data[$k1]["ProductOption"][] = array(
                    "id" => $dispensary_inventory_data[$k1]["ProductOption"]["id"],
                    "value" => $dispensary_inventory_data[$k1]["ProductOption"]["value"],
                    "unit" => $dispensary_inventory_data[$k1]["ProductOption"]["unit"]
                );
                unset($dispensary_inventory_data[$k1]["Product"]["id"]);
                unset($dispensary_inventory_data[$k1]["Product"]["name"]);
                unset($dispensary_inventory_data[$k1]["Product"]["city"]);
                unset($dispensary_inventory_data[$k1]["Product"]["quantity"]);
                unset($dispensary_inventory_data[$k1]["ProductOption"]["id"]);
                unset($dispensary_inventory_data[$k1]["ProductOption"]["value"]);
                unset($dispensary_inventory_data[$k1]["ProductOption"]["unit"]);
            }
            unset($dispensary_inventory_data[$k2]);
        }
    }
}   
php arrays multidimensional-array grouping sub-array
3个回答
1
投票

澄清后更新

使用此功能将数组转换为您需要的格式:

function my_format($data)
{
    $data2 = array();
    foreach($data as $val)
    {
         $keys = array_keys($val);
         // you can also use $val['DispensaryInventory']['product_id'] here,        
         // but i take it these refer to the same thing
         $product_id = $val['Product']['id'];
         if (!isset($data2[$product_id])) $data2[$product_id] = array();
         foreach($keys as $key)
         {
               if ( 'Product' === $key ) $data2[$product_id][$key] = $val[$key];
               elseif ( !isset($data2[$product_id][$key]) ) $data2[$product_id][$key] = array($val[$key]);
               else $data2[$product_id][$key][] = $val[$key];
         }
    }
    return array_values($data2); // you can also return the $data2 here
}

像这样使用:

$new_data = my_format($data);

(预期)输出(未测试)

Array
(
    [0] => Array
        (
            [DispensaryInventory] => Array
                (
                  [0] => Array
                       (
                            [id] => 15
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 
                            [price] => 
                       )
                  [1] => Array
                       (
                            [id] => 11
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 17
                            [price] => 12.00
                       )
                  [2] => Array
                       (
                             [id] => 12
                             [dispensary_id] => 1
                             [product_id] => 9
                             [quantity] => 
                             [price] =>
                       )
                  [3] => Array
                       (
                            [id] => 13
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 5
                            [price] => 123.00 
                       )
                  [4] => Array
                       (
                            [id] => 14
                            [dispensary_id] => 1
                            [product_id] => 9
                            [quantity] => 
                            [price] => 
                       )

                )

            [ProductOption] => Array
                (
                   [0] => Array
                       (
                            [value] => 2
                            [unit] => oz
                       )
                   [1] => Array
                       (
                            [value] => 1/8
                            [unit] => oz
                       )
                   [2] => Array
                       (
                            [value] => 1/4
                            [unit] => oz
                       )
                   [3] => Array
                       (
                            [value] => 1/2
                            [unit] => oz
                       )
                   [4] => Array
                       (
                            [value] => 1
                            [unit] => oz
                       )

                )

            [Product] => Array
                (
                    [id] => 9
                    [name] => Bajaj
                )

        )

    [1] => Array
        (
            [DispensaryInventory] => Array
                (
                  [0] => Array
                       (
                            [id] => 5
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 
                            [price] =>  
                       )
                  [1] => Array
                       (
                            [id] => 1
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 2
                            [price] => 123.00
                       )
                  [2] => Array
                       (
                             [id] => 2
                             [dispensary_id] => 1
                             [product_id] => 8
                             [quantity] => 
                             [price] => 
                       )
                  [3] => Array
                       (
                            [id] => 3
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 23
                            [price] => 222.00
                       )
                  [4] => Array
                       (
                            [id] => 4
                            [dispensary_id] => 1
                            [product_id] => 8
                            [quantity] => 12
                            [price] => 232.00
                       )

                )

            [ProductOption] => Array
                (
                   [0] => Array
                       (
                            [value] => 2
                            [unit] => oz
                       )
                   [1] => Array
                       (
                            [value] => 1/8
                            [unit] => oz
                       )
                   [2] => Array
                       (
                            [value] => 1/4
                            [unit] => oz
                       )
                   [3] => Array
                       (
                            [value] => 1/2
                            [unit] => oz
                       )
                   [4] => Array
                       (
                            [value] => 1
                            [unit] => oz
                       )

                )

            [Product] => Array
                (
                    [id] => 8
                    [name] => BMW
                )

        )    

    [2] => Array
        (
            [DispensaryInventory] => Array
                (
                  [0] => Array
                       (
                             [id] => 6
                             [dispensary_id] => 1
                             [product_id] => 3
                             [quantity] => 3
                             [price] => 21.00  
                       )
                  [1] => Array
                       (
                            [id] => 7
                            [dispensary_id] => 1
                            [product_id] => 3
                            [quantity] => 12
                            [price] => 56.00
                       )
                  [2] => Array
                       (
                             [id] => 2
                             [id] => 8
                             [dispensary_id] => 1
                             [product_id] => 3
                             [quantity] => 
                             [price] =>  
                       )
                  [3] => Array
                       (
                            [id] => 9
                            [dispensary_id] => 1
                            [product_id] => 3
                            [quantity] => 15
                            [price] => 354.00
                       )
                  [4] => Array
                       (
                            [id] => 10
                            [dispensary_id] => 1
                            [product_id] => 3
                            [quantity] => 
                            [price] =>
                       )

                )

            [ProductOption] => Array
                (
                   [0] => Array
                       (
                            [value] => 2
                            [unit] => oz
                       )
                   [1] => Array
                       (
                            [value] => 1/8
                            [unit] => oz
                       )
                   [2] => Array
                       (
                            [value] => 1/4
                            [unit] => oz
                       )
                   [3] => Array
                       (
                            [value] => 1/2
                            [unit] => oz
                       )
                   [4] => Array
                       (
                            [value] => 1
                            [unit] => oz
                       )

                )

            [Product] => Array
                (
                    [id] => 3
                    [name] => Yash product
                )

        )        

1
投票

您可以使用 php array_column 函数并将数组重新组织为具有所需结构和最少工作量的新数组:

$result = array();
$result['DispensaryInventory'] = array_column($data, 'DispensaryInventory');
$result['ProductOption'] = array_column($data, 'ProductOption');
$result['Product'] = array_column($data, 'Product');

0
投票

我建议通过将引用推入结果数组来按

product_id
值进行分组。 如果之前没有遇到过特定的product_id,则根据需要声明第一个数据集,并通过引用将该数据集推送到结果数组中。 当随后遇到特定的product_id时,将新数据推送到引用而不是结果数组中。 这样做的好处是不将临时键应用于结果数组。

Product
数据在第一次遇到后不会改变,并且您要求将此子数组保留为单个条目,以便以后不会将数据添加到引用变量中。

代码:(演示

$result = [];
foreach ($array as $set) {
    $group = $set['DispensaryInventory']['product_id'];
    if (!isset($ref[$group])) {
        $ref[$group] = [
            'DispensaryInventory' => [$set['DispensaryInventory']], // make into a subarray
            'ProductOption' => [$set['ProductOption']], // make into a subarray
            'Product' => $set['Product']  // leave as-id
        ];
        $result[] =& $ref[$group];  // push reference into result array
    } else {
        $ref[$group]['DispensaryInventory'][] = $set['DispensaryInventory'];  // push new data into subarray
        $ref[$group]['ProductOption'][] = $set['ProductOption'];  // push new data into subarray
    }
}
var_export($result);
© www.soinside.com 2019 - 2024. All rights reserved.