简体   繁体   English

在同一键 php 中合并具有相同值的数组

[英]Merge array with same value in same key php

I have this code:我有这个代码:

$order = wc_get_order( 988613 );

$product_array = array();
$daty = array();
$counter = 1;
foreach ($order->get_items() as $item_key => $item ){
    
    $daty_dostaw = $item->get_meta('Daty dostaw');
    $product_id = $item->get_product_id();
    
    $daty_dostaw_explode = explode(',',$daty_dostaw);
    
    
    $daty[$counter]['product_id'] = $product_id;
    
    
    foreach($daty_dostaw_explode as $key => $data){
        $daty[$counter]['data'][] = $data;
    }
    $counter++; 
}

When I print it it shows me this当我打印它时,它告诉我这个

    Array
    (
    [1] => Array
        (
            [product_id] => 988012
            [data] => Array
                (
                    [0] => 13-08-2022
                    [1] => 25-08-2022
                    [2] => 30-08-2022
                )

        )

    [2] => Array
        (
            [product_id] => 988087
            [data] => Array
                (
                    [0] => 25-08-2022
                    [1] => 31-08-2022
                    [2] => 30-09-2022
                )

        )

    )

I want to combine an array that have the same dates to display like this:我想组合一个具有相同日期的数组以显示如下:

Array
(   [1] => Array
        (
            [product_id] => array(988012, 988087)
            [data] => Array
                (
                    [0] => 25-08-2022
                )

        )

    [2] => Array
        (
            [product_id] => 988012
            [data] => Array
                (
                    [0] => 13-08-2022
                    [1] => 30-08-2022
                )

        )

    [3] => Array
        (
            [product_id] => 988087
            [data] => Array
                (
                    [0] => 31-08-2022
                    [1] => 30-09-2022
                )

        )

)

I want to merge the array those with the same dates.我想合并具有相同日期的数组。 I don't know how to explain it exactly, above I showed what I would like to achieve.我不知道如何准确地解释它,上面我展示了我想要实现的目标。 I have already written thousands of lines of code using foreach and have not been able to achieve this:(我已经使用 foreach 编写了数千行代码并且无法实现这一点:(

Basically you want to convert product_id into an array if there are multiple products with the same date.基本上,如果有多个产品具有相同的日期,您希望将product_id转换为数组。 I would search for such matches, create the merges, remove the elements from the individual occurrence or even the individual occurrence if it gets emptied.我会搜索这样的匹配,创建合并,从单个事件中删除元素,如果它被清空,甚至是单个事件。 An example would be this:一个例子是这样的:

$cachedDates = []; //Here we will store the dates we have gone through so far
foreach ($product_array as $key => $product) {
    foreach ($product_array[$key]["data"] => $date) {
        if (!in_array($date, $cachedDates)) {
            //Creating a cache entry for this date
            $cachedDates[$date] = ["keys" => [], "values" => []];
        }
        //Adding the key and product to the date's cache
        $cachedDates[$date]["keys"][]= $key;
        $cachedDates[$date]["values"][]= $product["product_id"];
    }
}

//Adding the new items to $product_array and cleaning up items that become redundant
foreach ($cachedDates as $date => $entry) {
    //We only merge items that have a not-unique date
    if (count($entry["values"]) > 0) {
        //adding a new item with the correct values and the date
        $product_array[]= [
            "product_id" => $entry["values"],
            "data" => $date
        ];
        //Cleanup
        for ($index = 0; $index < count($entry["keys"]); $index++) {
            //We remove the date from the data entry after finding its index
            $removeIndex = array_search($date, $product_array[$entry["keys"][$index]]["data"]);
            unset($product_array[$entry["keys"][$index]]["data"][$removeIndex]);
            //If the data array is empty, then the whole product was already merged into some other item(s)
            if (!count($product_array[$entry["keys"][$index]]["data"])) {
                unset($product_array[$entry["keys"][$index]]);
            }
        }
    }
}

The code above was untested, if you find issues with it and errors, then provide the appropriate PHP input instead of its print, even a json_encode result is good-enough上面的代码未经测试,如果您发现它的问题和错误,则提供适当的 PHP 输入而不是其打印,即使是 json_encode 结果也足够好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM