Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中按对象值之和对多维数组排序_Php_Arrays_Sorting - Fatal编程技术网

PHP中按对象值之和对多维数组排序

PHP中按对象值之和对多维数组排序,php,arrays,sorting,Php,Arrays,Sorting,我有以下多维产品阵列。每个阵列是一对产品,构成一个产品集,我需要按每个产品集的总价订购多维阵列 array(4) { [0]=> array(2) { ["product1"]=> object(stdClass)#5075 (2) { ["product_id"]=> string(4) "9416" ["price"]=> string(6) "110.00" } ["produc

我有以下多维产品阵列。每个阵列是一对产品,构成一个产品集,我需要按每个产品集的总价订购多维阵列

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的总和按升序对多维数组进行排序。例如[1]应该高于[0],因为75+62小于110+100

如果有人能帮助我,我们将不胜感激。

您可以使用
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');
函数比较价格($a,$b)
{
$a_价格=$a['product1']->价格+$a['product2']->价格;
$b_price=$b['product1']->price+$b['product2']->price;
如果($a_价格==$b_价格)返回0;

return($a_price您需要使用用户定义的排序

usort($products,function($a,$b){
$prodA=$a['product1']['price']+$a['product2']['price'];
$prodB=$b['product1']['price']+$b['product2']['price'];
如果($prodA==$prodB)返回0;
回报($prodA<$prodB)?-1:1;
});
php7+“spaceship操作符”(也称为三向比较操作符)使
usort()的语法尽可能简洁明了

代码:()


您好,欢迎来到stackoverflow,请在您的问题中解释到目前为止您在相关代码方面做了哪些尝试,以及哪些不起作用。请参阅:感谢您的回答。对此没有多少乐趣,得到PHP错误:未定义属性:stdClass::$price我正在使用CodeIgniter,数组是一个uld内部有5-10个数组。我不需要以某种方式循环这个usort函数吗?@JamesD是的,那么您必须在
comparePrice()内部循环以计算两个价格。(正如您所说,数组中可以有n个价格)@JamesD另外,我的代码中没有
$price
,如果您使用我的代码,您如何获得与错误相关的
$price
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;
});
$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',
    ),
  ),
)