1

I have the below multi dimensional array of products. Each array is a pair of products that make a product set, I need to order the multi dimensional array by the total price of each product set.

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"
    }
  }
}

I need to sort the multi-dimensional array by the total sum of product1 + product2 in each array in ascending order. For example [1] should be above [0] as 75+62 is less than 110 +100.

If anyone can help me with this it would be greatly appreciated.

1
  • Hello and welcome to stackoverflow, please also explain in your question what you have tried so far with relevant code and what did not work about this. Please refer to: stackoverflow.com/help/how-to-ask Commented Oct 12, 2018 at 10:31

3 Answers 3

3

You can use usort() for this purpose:-

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 hardcoded working example:- https://3v4l.org/mTfu6

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. Not getting much joy with this, getting PHP errors: Undefined property: stdClass::$price I am using CodeIgniter and the array is a database result that could have 5-10 arrays inside it. Would I not need to loop over this usort function somehow?
@JamesD yes then you have to loop inside comparePrice() for calculating two prices. (As you said that array can have n number of prices in it)
@JamesD also i don't have $price in my code , so how's you are getting error related $price if you are using my code
0

You need to use user-defined sorting

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;
});

Comments

0

The php7+ "spaceship operator" (aka three-way-comparison operator) makes the syntax with usort() as clean and brief as possible.

Code: (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);

Output:

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',
    ),
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.