php多数组分组和最大值每一组和总和每一数组

nue99wik  于 2023-03-16  发布在  PHP
关注(0)|答案(1)|浏览(121)

php多数组分组Array [0]和每组最大Array [3]值。以下数组表示Array [0]- id,Array [1]- price,Array [2]- product name,Array [3]- shipping price,Array [4]- shipping method每个数组需要按id分组,然后对每组价格求和,并使用最大运费。输出如下所示。

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 2200.00
            [2] => Portable Hard Drive
            [3] => 120.00
            [4] => Taxi
        )

    [1] => Array
        (
            [0] => 2
            [1] => 8500.00
            [2] => HP Laptop
            [3] => 80.00
            [4] => Taxi
        )

    [2] => Array
        (
            [0] => 1
            [1] => 700.00
            [2] => Luxury Watch
            [3] => 200.00
            [4] => TAXI FAST
        )

    [3] => Array
        (
            [0] => 2
            [1] => 1500.00
            [2] => VGA DISTRIBUTE
            [3] => 300.00
            [4] => EMS
        )

    [4] => Array
        (
            [0] => 1
            [1] => 5000.00
            [2] => 3D Camera
            [3] => 150.00
            [4] => DHL
        )
)

输出列表如下所示,也可以放入数组中

1-5000.00-3D Camera-150.00-DHL
1-700.00-Luxury Watch-200.00-TAXI FAST
Total price = 5700.00
Max of ship = 200.00
Grand total = 5900.00
2-2200.00-Portable Hard Drive-120.00-taxi
2-8500.00-HP Laptop-80.00-Taxi
2-1500.00-VGA DISTRIBUTE-300.00-EMS
Total price = 12200.00
Max of ship = 300.00
Grand total = 12500.00
This order Total price 17900.00
This order Total max ship 500.00
This order Grand Total 18400.00
cngwdvgl

cngwdvgl1#

要实现这一点,除了foreach之外,您不需要太多的array_columnarray_summax
把任务分成几个小任务:

  • 从初始列表构建结果数据保持器
  • 按ID将项目分组
  • 按id排序组
  • 迭代组和计算每个组的总价格,最大航运和总计
  • 我也更新总价格和总最大船舶同时处理每个组
  • 计算总数
  • 显示您想要的数据,因为您的结构已经包含了所有需要的数据
<?php

$input = [
    0 => [
            0 => 2,
            1 => 2200.00,
            2 => 'Portable Hard Drive',
            3 => 120.00,
            4 => 'Taxi'
        ],
    1 => [
            0 => 2,
            1 => 8500.00,
            2 => 'HP Laptop',
            3 => 80.00,
            4 => 'Taxi',
        ],
    2 => [
            0 => 1,
            1 => 700.00,
            2 => 'Luxury Watch',
            3 => 200.00,
            4 => 'TAXI FAST',
        ],
    3 => [
            0 => 2,
            1 => 1500.00,
            2 => 'VGA DISTRIBUTE',
            3 => 300.00,
            4 => 'EMS',
        ],
    4 => [
            0 => 1,
            1 => 5000.00,
            2 => '3D Camera',
            3 => 150.00,
            4 => 'DHL',
        ]
];

const ID = 0;
const PRICE = 1;
const PRODUCT_NAME = 2;
const SHIPPING_PRICE = 3;
const SHIPPING_METHOD = 4;

// build initial result, populate groups (items groupped by id)
$result = [
  'groups' => [],
  'totalPrice' => 0,
  'shippingPriceMax' => 0,
];
foreach($input as $item) {
  $result['groups'][$item[ID]]['products'][] = $item;
}

// sord ids descending
ksort($result['groups'], SORT_NUMERIC);

var_dump($result);

// for every group calculate total price, max shipping and grand total
foreach($result['groups'] as $id => $group) {
  $result['groups'][$id]['totalPrice'] = array_sum(array_column($group['products'], PRICE));
  $result['groups'][$id]['shippingPriceMax'] = max(array_column($group['products'], SHIPPING_PRICE));
  $result['groups'][$id]['grandTotal'] = $result['groups'][$id]['totalPrice'] + $result['groups'][$id]['shippingPriceMax'];

  // update also total values for all groups
  $result['totalPrice'] += $result['groups'][$id]['totalPrice'];
  $result['shippingPriceMax'] += $result['groups'][$id]['shippingPriceMax'];
}
$result['grandTotal'] = $result['totalPrice'] + $result['shippingPriceMax'];

var_dump($result);

// build the output
$output = '';
foreach($result['groups'] as $id => $group) {
  foreach($group['products'] as $product) {
    $output .= $product[ID].'-'.number_format($product[PRICE],2,'.','').'-'.$product[PRODUCT_NAME].'-'.number_format($product[SHIPPING_PRICE],2,'.','').'-'.$product[SHIPPING_METHOD].PHP_EOL;
  }
  
  $output .= 'Total price = '.number_format($group['totalPrice'],2,'.','').PHP_EOL;
  $output .= 'Max of ship = '.number_format($group['shippingPriceMax'],2,'.','').PHP_EOL;
  $output .= 'Grand total = '.number_format($group['grandTotal'],2,'.','').PHP_EOL;
}
$output .= 'This order Total price '.number_format($result['totalPrice'],2,'.','').PHP_EOL;
$output .= 'This order Total max ship '.number_format($result['shippingPriceMax'],2,'.','').PHP_EOL;
$output .= 'This order Grand Total '.number_format($result['grandTotal'],2,'.','');

var_dump($output);

按ID创建组并按ID排序后的初始$result:

array(3) {
  ["groups"]=>
  array(2) {
    [1]=>
    array(1) {
      ["products"]=>
      array(2) {
        [0]=>
        array(5) {
          [0]=>
          int(1)
          [1]=>
          float(700)
          [2]=>
          string(12) "Luxury Watch"
          [3]=>
          float(200)
          [4]=>
          string(9) "TAXI FAST"
        }
        [1]=>
        array(5) {
          [0]=>
          int(1)
          [1]=>
          float(5000)
          [2]=>
          string(9) "3D Camera"
          [3]=>
          float(150)
          [4]=>
          string(3) "DHL"
        }
      }
    }
    [2]=>
    array(1) {
      ["products"]=>
      array(3) {
        [0]=>
        array(5) {
          [0]=>
          int(2)
          [1]=>
          float(2200)
          [2]=>
          string(19) "Portable Hard Drive"
          [3]=>
          float(120)
          [4]=>
          string(4) "Taxi"
        }
        [1]=>
        array(5) {
          [0]=>
          int(2)
          [1]=>
          float(8500)
          [2]=>
          string(9) "HP Laptop"
          [3]=>
          float(80)
          [4]=>
          string(4) "Taxi"
        }
        [2]=>
        array(5) {
          [0]=>
          int(2)
          [1]=>
          float(1500)
          [2]=>
          string(14) "VGA DISTRIBUTE"
          [3]=>
          float(300)
          [4]=>
          string(3) "EMS"
        }
      }
    }
  }
  ["totalPrice"]=>
  int(0)
  ["shippingPriceMax"]=>
  int(0)
}

$result计算完所有需要的数据后(这包含了我们需要显示它的所有内容):

array(4) {
  ["groups"]=>
  array(2) {
    [1]=>
    array(4) {
      ["products"]=>
      array(2) {
        [0]=>
        array(5) {
          [0]=>
          int(1)
          [1]=>
          float(700)
          [2]=>
          string(12) "Luxury Watch"
          [3]=>
          float(200)
          [4]=>
          string(9) "TAXI FAST"
        }
        [1]=>
        array(5) {
          [0]=>
          int(1)
          [1]=>
          float(5000)
          [2]=>
          string(9) "3D Camera"
          [3]=>
          float(150)
          [4]=>
          string(3) "DHL"
        }
      }
      ["totalPrice"]=>
      float(5700)
      ["shippingPriceMax"]=>
      float(200)
      ["grandTotal"]=>
      float(5900)
    }
    [2]=>
    array(4) {
      ["products"]=>
      array(3) {
        [0]=>
        array(5) {
          [0]=>
          int(2)
          [1]=>
          float(2200)
          [2]=>
          string(19) "Portable Hard Drive"
          [3]=>
          float(120)
          [4]=>
          string(4) "Taxi"
        }
        [1]=>
        array(5) {
          [0]=>
          int(2)
          [1]=>
          float(8500)
          [2]=>
          string(9) "HP Laptop"
          [3]=>
          float(80)
          [4]=>
          string(4) "Taxi"
        }
        [2]=>
        array(5) {
          [0]=>
          int(2)
          [1]=>
          float(1500)
          [2]=>
          string(14) "VGA DISTRIBUTE"
          [3]=>
          float(300)
          [4]=>
          string(3) "EMS"
        }
      }
      ["totalPrice"]=>
      float(12200)
      ["shippingPriceMax"]=>
      float(300)
      ["grandTotal"]=>
      float(12500)
    }
  }
  ["totalPrice"]=>
  float(17900)
  ["shippingPriceMax"]=>
  float(500)
  ["grandTotal"]=>
  float(18400)
}

输出:

string(407) "1-700.00-Luxury Watch-200.00-TAXI FAST
1-5000.00-3D Camera-150.00-DHL
Total price = 5700.00
Max of ship = 200.00
Grand total = 5900.00
2-2200.00-Portable Hard Drive-120.00-Taxi
2-8500.00-HP Laptop-80.00-Taxi
2-1500.00-VGA DISTRIBUTE-300.00-EMS
Total price = 12200.00
Max of ship = 300.00
Grand total = 12500.00
This order Total price 17900.00
This order Total max ship 500.00
This order Grand Total 18400.00"

这与您的输出之间的唯一区别是在group id中:1我有Luxury Watch第一和3D Camera第二,这是你的输入数组的顺序。2顺序相同。

相关问题