【问题标题】:How to find the minimum value in a multi dimensional array matching another value如何在与另一个值匹配的多维数组中找到最小值
【发布时间】:2018-06-22 06:40:09
【问题描述】:

我有一个这样的多维数组,

[0] => Array ( [contractId] => 1 [pricingDetails] => Array ( 
                    [0] => Array ( [pricingName] => Price1 [priceId] => 1 [ageGroup] => 1 [ageFrom] => 13 [ageTo] => 50 [price] => 110.00 ) 
                    [1] => Array ( [pricingName] => Price2 [priceId] => 2 [ageGroup] => 1 [ageFrom] => 13 [ageTo] => 50 [price] => 77.00 ) 
                    [2] => Array ( [pricingName] => Price3 [priceId] => 3 [ageGroup] => 2 [ageFrom] => 3  [ageTo] => 12 [price] => 30.00 ) 
                    [3] => Array ( [pricingName] => Price4 [priceId] => 4 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 30.00 ) 
                    [4] => Array ( [pricingName] => Price5 [priceId] => 5 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 11.00 ) 
                    [5] => Array ( [pricingName] => Price6 [priceId] => 6 [ageGroup] => 2 [ageFrom] => 0 [ageTo] => 2 [price] => 0.00 ) 
                    [6] => Array ( [pricingName] => Price7 [priceId] => 7 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 40.00 ) 
                    [7] => Array ( [pricingName] => Price8 [priceId] => 8 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 30.00 ) 
                    [8] => Array ( [pricingName] => Price9 [priceId] => 9 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 80.00 ) ) 
[1] => Array ( [contractId] => 2 [pricingDetails] => Array ( 
                    [0] => Array ( [pricingName] => Price1 [priceId] => 10 [ageGroup] => 1 [ageFrom] => 13 [ageTo] => 50 [price] => 120.00 ) 
                    [1] => Array ( [pricingName] => Price2 [priceId] => 11 [ageGroup] => 1 [ageFrom] => 13 [ageTo] => 50 [price] => 80.00 ) 
                    [2] => Array ( [pricingName] => Price3 [priceId] => 12 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 35.00 ) 
                    [3] => Array ( [pricingName] => Price4 [priceId] => 13 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 35.00 ) 
                    [4] => Array ( [pricingName] => Price5 [priceId] => 14 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 15.00 ) 
                    [5] => Array ( [pricingName] => Price6 [priceId] => 15 [ageGroup] => 2 [ageFrom] => 0 [ageTo] => 2 [price] => 0.00 ) 
                    [6] => Array ( [pricingName] => Price7 [priceId] => 16 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 45.00 ) 
                    [7] => Array ( [pricingName] => Price8 [priceId] => 17 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 35.00 ) 
                    ) ) ))

我需要创建一个完全相似的数组,但只包含每个年龄段(1,2 和 3)的最低价格值,对于第 2 年龄段,会有更多(每个年龄段的 0-2 和 3-12例如)基于不同的年龄范围(ageFrom 和 ageTo)。

数组结构的其余部分将保持不变。

输出数组会是这样的,

[0] => Array ( [contractId] => 1 [pricingDetails] => Array (  
                    [0] => Array ( [pricingName] => Price2 [priceId] => 2 [ageGroup] => 1 [ageFrom] => 13 [ageTo] => 50 [price] => 77.00 ) 
                    [1] => Array ( [pricingName] => Price5 [priceId] => 5 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 11.00 ) 
                    [2] => Array ( [pricingName] => Price6 [priceId] => 6 [ageGroup] => 2 [ageFrom] => 0 [ageTo] => 2 [price] => 0.00 ) 
                    [3] => Array ( [pricingName] => Price8 [priceId] => 8 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 30.00 )  
[1] => Array ( [contractId] => 2 [pricingDetails] => Array ( 
                    [0] => Array ( [pricingName] => Price2 [priceId] => 11 [ageGroup] => 1 [ageFrom] => 13 [ageTo] => 50 [price] => 80.00 ) 
                    [1] => Array ( [pricingName] => Price5 [priceId] => 14 [ageGroup] => 2 [ageFrom] => 3 [ageTo] => 12 [price] => 15.00 ) 
                    [2] => Array ( [pricingName] => Price6 [priceId] => 15 [ageGroup] => 2 [ageFrom] => 0 [ageTo] => 2 [price] => 0.00 ) 
                    [3] => Array ( [pricingName] => Price8 [priceId] => 17 [ageGroup] => 3 [ageFrom] => 51 [ageTo] => 149 [price] => 35.00 ) 
                    ) ) ))

【问题讨论】:

  • 你能发布一个var_export而不是数组的print_r吗?到目前为止你做了什么?
  • 代码在哪里?

标签: php arrays multidimensional-array


【解决方案1】:

这个 OP 有不接受任何答案的历史,但无论如何我都会回答。我要做的是创建一个分组名称,这样我就可以选择哪个年龄段的价格最低。

我会为此创建一个临时容器。一旦它们被分组,在批次结束时。我会将它展平,然后用新分组的数组覆盖数组。

这是我的想法:

foreach ($array as $key => &$value) {
    $temp = array(); // create temporary grouping container for pricing details
    foreach ($value['pricingDetails'] as $pricingDetails) {
        $age_range = $pricingDetails['ageFrom'] . ' - ' . $pricingDetails['ageTo']; // this is the grouping name range that will be used in order to group each ranges
        if (
            empty($temp[$pricingDetails['ageGroup']][$age_range]) || // if not yet set
            $pricingDetails['price'] < $temp[$pricingDetails['ageGroup']][$age_range]['price'] // or if current iteration price lower than last price
        ) {
            $temp[$pricingDetails['ageGroup']][$age_range] = $pricingDetails; // assign it
        }
    }
    $temp = array_map('array_values', $temp); // key reindexing
    $new_pricing = array_reduce($temp, function ($carry, $item) { // combine all batches
        return array_merge($carry, $item);
    }, []);
    // end of batch, apply the new grouping
    $value['pricingDetails'] = $new_pricing; // assign new pricing
}

这是一个示例fiddle

如果有其他方法,请告诉我。

【讨论】:

  • 对不起,我只是发布了问题,而不是尝试的代码。不过你的好像不错。将进一步检查。
猜你喜欢
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多