按PHP中对象值的总和对多维数组进行排序

时间:2018-10-12 10:26:53

标签: php arrays sorting

我有下面的多维产品数组。每个数组都是构成一个产品集的一对产品,我需要按每个产品集的总价订购多维数组。

array(4) {
  [0]=>
  array(2) {
    ["product1"]=>
    object(stdClass)#5075 (2) {
      ["product_id"]=>
      string(4) "9416"
      ["price"]=>
      string(6) "110.00"
    }
    ["product2"]=>
    object(stdClass)#5077 (2) {
      ["product_id"]=>
      string(4) "9431"
      ["price"]=>
      string(6) "100.00"
    }
  }
  [1]=>
  array(2) {
    ["product1"]=>
    object(stdClass)#5065 (2) {
      ["product_id"]=>
      string(4) "1254"
      ["price"]=>
      string(6) "75.00"
    }
    ["product2"]=>
    object(stdClass)#5067 (2) {
      ["product_id"]=>
      string(4) "9431"
      ["price"]=>
      string(6) "62.00"
    }
  }
  [2]=>
  array(2) {
    ["product1"]=>
    object(stdClass)#5055 (2) {
      ["product_id"]=>
      string(4) "9416"
      ["price"]=>
      string(6) "45.00"
    }
    ["product2"]=>
    object(stdClass)#5057 (2) {
      ["product_id"]=>
      string(4) "9431"
      ["price"]=>
      string(6) "50.00"
    }
  }
  [3]=>
  array(2) {
    ["product1"]=>
    object(stdClass)#5045 (2) {
      ["product_id"]=>
      string(4) "9416"
      ["price"]=>
      string(6) "60.00"
    }
    ["product2"]=>
    object(stdClass)#5047 (2) {
      ["product_id"]=>
      string(4) "9431"
      ["price"]=>
      string(6) "25.00"
    }
  }
}

我需要按升序按每个数组中product1 + product2的总和对多维数组进行排序。例如[75]小于110 +100时[1]应该高于[0]。

如果有人可以帮助我,将不胜感激。

3 个答案:

答案 0 :(得分:3)

您可以为此目的使用usort():-

function comparePrice($a,$b)
{
  $a_price = $a['product1']->price + $a['product2']->price;
  $b_price = $b['product1']->price + $b['product2']->price;
  if ($a_price ==$b_price) return 0;
  return ($a_price<$b_price)? -1:1;
}
usort($array,'comparePrice');

一个硬编码的工作示例:-https://3v4l.org/mTfu6

答案 1 :(得分:0)

您需要使用用户定义的排序方式

http://php.net/manual/en/function.usort.php

usort($products, function($a, $b) {
    $prodA = $a['product1']['price'] + $a['product2']['price'];
    $prodB = $b['product1']['price'] + $b['product2']['price'];

    if($prodA == $prodB) return 0;
    return ($prodA < $prodB) ? -1 : 1;
});

答案 2 :(得分:0)

php7 +“飞船运算符”(又称三向比较运算符)使usort()的语法尽可能简洁明了。

代码:(Demo

$array = [
    [
        "product1" => (object) ["product_id" => "9416", "price"=>"110.00"],
        "product2" => (object) ["product_id"=>"9431", "price"=>"100.00"]
    ],
    [
        "product1" => (object) ["product_id" => "1254", "price"=>"75.00"],
        "product2" => (object) ["product_id"=>"9431", "price"=>"62.00"]
    ],
    [
        "product1" => (object) ["product_id" => "9416", "price"=>"45.00"],
        "product2" => (object) ["product_id"=>"9431", "price"=>"50.00"]
    ],
    [
        "product1" => (object) ["product_id" => "9416", "price"=>"60.00"],
        "product2" => (object) ["product_id"=>"9431", "price"=>"25.00"]
    ]
];

usort($array, function($a, $b) {
    return $a['product1']->price + $a['product2']->price <=> $b['product1']->price + $b['product2']->price;
});

var_export($array);

输出:

array (
  0 =>                                // sum = 85.00
  array (
    'product1' => 
    (object) array(
       'product_id' => '9416',
       'price' => '60.00',
    ),
    'product2' => 
    (object) array(
       'product_id' => '9431',
       'price' => '25.00',
    ),
  ),
  1 =>                                // sum = 95.00 
  array (
    'product1' => 
    (object) array(
       'product_id' => '9416',
       'price' => '45.00',
    ),
    'product2' => 
    (object) array(
       'product_id' => '9431',
       'price' => '50.00',
    ),
  ),
  2 =>                                // sum = 137.00 
  array (
    'product1' => 
    (object) array(
       'product_id' => '1254',
       'price' => '75.00',
    ),
    'product2' => 
    (object) array(
       'product_id' => '9431',
       'price' => '62.00',
    ),
  ),
  3 =>                                // sum = 210.00 
  array (
    'product1' => 
    (object) array(
       'product_id' => '9416',
       'price' => '110.00',
    ),
    'product2' => 
    (object) array(
       'product_id' => '9431',
       'price' => '100.00',
    ),
  ),
)