0

Here is my Array:

Array
(
    [0] => Array
        (
            [MyProduct] => Array
                (
                    [id] => 5681
                    [part_number] => 78689
                    [model] => Tiger Paw GTZ All Season
                    [web_price] => $74.68
                    [web_price_markup] => 91.07

                )
        )

    [1] => Array
        (
            [MyProduct] => Array
                (
                    [id] => 33370
                    [part_number] => 49946
                    [model] => Tiger Paw GTZ A/S 2
                    [web_price] => $75.92
                    [web_price_markup] => 92.58
                )
        )

    [2] => Array
        (
            [MyProduct] => Array
                (
                    [id] => 12542
                    [part_number] => 28953781
                    [model] => Ziex ZE950 A/S
                    [web_price] => $78.86
                    [web_price_markup] => 96.17                    
                )
        )       
)

I want to sort this array by "web_price_markup", please help.

Eddie
  • 26,593
  • 6
  • 36
  • 58
  • 2
    `usort()` and a comparator. – Aniket Sahrawat Mar 21 '18 at 08:15
  • Please refere this https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields – Arosha De Silva Mar 21 '18 at 08:16
  • 1
    This question translates to: “I don't want to do my work, please solve this problem for me, as I'm lazy.” – feeela Mar 21 '18 at 08:17
  • Possible duplicate to https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – feeela Mar 21 '18 at 08:18
  • duplicated question. please read the following. [answer](https://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value) – Wils Mar 21 '18 at 08:20
  • 2
    Possible duplicate of [PHP Sort Array By SubArray Value](https://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value) – Wils Mar 21 '18 at 08:21
  • @feeela, thank you for the translation, for me its just one of the day when you try but nothing is working. thanx..! –  Mar 21 '18 at 09:21

1 Answers1

0

First of all write comparer function which compares 'web_price_markup' values of your object.

function compare($a,$b){   return strcasecmp($a['name'], $b['name']); }

and then use usort (assume that your array is $x):

if(usort($x, 'compare')){
         var_dump($x);
    }
godot
  • 3,422
  • 6
  • 25
  • 42