0

I have this array i want sorting using same preview_url and same countries code and want result which array have higher amount of value and unmatch array data remain same. I want result i have add in comment please provide sollution for same i have stuck on this issue from couple of days

Thanks in advance

Array
(
    [id] => 377556        
    [amount] => 1.46000
    [preview_url] => https://itunes.apple.com/app/id543186831?mt=8
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states
                    [regions] => Array
                        (
                        )
                )
        )
)
Array
(
    [id] => 377557        
    [amount] => 2.46000
    [preview_url] => https://itunes.apple.com/app/id543186831?mt=8
    [countries] => Array
        (
            [US] => Array
                (
                    [id] => 840
                    [code] => US
                    [name] => united states                        
                )
            [UK] => Array
                (
                    [id] => 841
                    [code] => UK
                    [name] => united kingdom
                )    
        )
)
Er Nilay Parekh
  • 569
  • 5
  • 17
  • i want this result Array ( [id] => 377557 [amount] => 2.46000 [preview_url] => https://itunes.apple.com/app/id543186831?mt=8[countries] => Array ( [US] => Array ( [id] => 840 [code] => US [name] => united states) [UK] => Array ( [id] => 841 [code] => UK [name] => united kingdom [regions] => Array ( ) ) ) ) – Er Nilay Parekh Mar 30 '17 at 11:42

2 Answers2

0

You can sort it by using usort.

usort($array, function($a, $b) {
     return $a['amount'] < $b['amount'];
});

For details refer this great answer by christian. Kudos

Happy Coding.

Community
  • 1
  • 1
Touheed Khan
  • 2,149
  • 16
  • 26
0

You have to use multisort

$mylist = array(
array('ID' => 1, 'title' => 'Boring Meeting', 'event_type' => 'meeting'),
array('ID' => 2, 'title' => 'Find My Stapler', 'event_type' => 'meeting'),
array('ID' => 3, 'title' => 'Mario Party', 'event_type' => 'party'),
array('ID' => 4, 'title' => 'Duct Tape Party', 'event_type' => 'party')
);
# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($mylist as $k=>$v) {
$sort['title'][$k] = $v['title'];
$sort['event_type'][$k] = $v['event_type'];
}
# sort by event_type desc and then title asc
array_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist);
$mylist = array(
array('ID' => 1, 'title' => 'Boring Meeting', 'event_type' => 'meeting'),
array('ID' => 2, 'title' => 'Find My Stapler', 'event_type' => 'meeting'),
array('ID' => 3, 'title' => 'Mario Party', 'event_type' => 'party'),
array('ID' => 4, 'title' => 'Duct Tape Party', 'event_type' => 'party')
);
# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($mylist as $k=>$v) {
$sort['title'][$k] = $v['title'];
$sort['event_type'][$k] = $v['event_type'];
}
# sort by event_type desc and then title asc
array_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist);

now my array will

array (
0 => 
array (
'ID' => 4,
'title' => 'Duct Tape Party',
'event_type' => 'party',
),
1 => 
array (
'ID' => 3,
'title' => 'Mario Party',
'event_type' => 'party',
),
2 => 
array (
'ID' => 1,
'title' => 'Boring Meeting',
'event_type' => 'meeting',
),
3 => 
array (
'ID' => 2,
'title' => 'Find My Stapler',
'event_type' => 'meeting',
),
)