Sort array using array_multisort
Tag : php , By : user92243
Date : March 29 2020, 07:55 AM
it should still fix some issue OK, so, one of the first solutions that comes to mind is adding in the empty values to make them consistent: function array_multisort_by_order(array $array, $by, array $order)
{
$max = max(array_map('count',$array));
//or, alternatively, depending on input (if there are no 'complete' subarrays):
//$max = max(array_map(function($arr){return max(array_keys($arr));},$array))+1;
//ADDITION: negative numeric keys:
$min = min(array_map(function($arr){return min(array_keys($arr));},$array));
$width = $max - min(0,$min);
foreach($array as &$sub){
// $addin = array_diff_key(array_fill(0,$max,null),$sub);
// $addin changed for negative keys:
$addin = array_diff_key(array_combine(range($min,$max),array_fill(0,$width,null)),$sub);
$sub = $addin + $sub;
ksort($sub);
}
$order = array_flip($order);
$params[] = $array[$by];
foreach($params[0] as &$v) $v = $order[$v];
foreach($array as &$v) $params[] = &$v; unset($v);
call_user_func_array('array_multisort', $params);
//no closeures here:
//foreach($array as &$sub) $sub = array_filter(function($a){return !is_null($a);},$sub);
$filter = create_function('$a','return !is_null($a);');
foreach($array as &$sub) $sub = array_filter($sub,$filter);
return $array;
}
|
PHP array_multisort not sorting my multidimensional array as expected
Date : March 29 2020, 07:55 AM
wish help you to fix your issue With array Multisort, you can Sort depending on a column. This is a example, where you can easily sort by "all" available columns: <?
$data['test1'] = array('volume' => 67, 'edition' => 2, 'name' => 6, 'type' => 2);
$data['test2'] = array('volume' => 86, 'edition' => 1, 'name' => 7, 'type' => 2);
$data['test3'] = array('volume' => 85, 'edition' => 6, 'name' => 8, 'type' => 2);
$data['test4'] = array('volume' => 98, 'edition' => 2, 'name' => 9, 'type' => 2);
$data['test5'] = array('volume' => 86, 'edition' => 6, 'name' => 10, 'type' => 2);
$data['test6'] = array('volume' => 67, 'edition' => 7, 'name' => 11, 'type' => 2);
//Create index rows
foreach ($data as $row) {
foreach ($row as $key => $value){
${$key}[] = $value; //Creates $volume, $edition, $name and $type arrays.
}
}
//ex: sort by edition asc, then by name DESC:
array_multisort($edition, SORT_ASC, $name, SORT_DESC, $data);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
Array
(
[test2] => Array
(
[volume] => 86
[edition] => 1
[name] => 7
[type] => 2
)
[test4] => Array
(
[volume] => 98
[edition] => 2
[name] => 9
[type] => 2
)
[test1] => Array
(
[volume] => 67
[edition] => 2
[name] => 6
[type] => 2
)
[test5] => Array
(
[volume] => 86
[edition] => 6
[name] => 10
[type] => 2
)
[test3] => Array
(
[volume] => 85
[edition] => 6
[name] => 8
[type] => 2
)
[test6] => Array
(
[volume] => 67
[edition] => 7
[name] => 11
[type] => 2
)
)
$letters = array("b","a","c");
$numbers = array(5,4,2);
$letters = array("b","a","a");
$numbers = array(5,4,2);
$items = array("a","b","z","a","z","z");
uasort($items, "sortByPredefinedOrder");
function sortByPredefinedOrder($leftItem, $rightItem){
$order = array("a","z","b","x"); //defined somewhere
$flipped = array_flip($order); //so we can access "position by value"
$leftPos = $flipped[$leftItem];
$rightPos = $flipped[$rightItem];
return $leftPos >= $rightPos;
}
print_r($items); //Array ( [0] => a [3] => a [2] => z [4] => z [5] => z [1] => b )
$leftPos = $flipped[$leftItem["volume"]];
$rightPos = $flipped[$rightItem["volume"]];
|
Using php array_multisort twice to sort the same array
Tag : php , By : Jet Thompson
Date : March 29 2020, 07:55 AM
wish of those help It's because your first multisort messed up the order of $rows. A dummy array should do the trick: $temp = $rows;
|
php array_multisort use array name as argument
Tag : php , By : user179863
Date : March 29 2020, 07:55 AM
around this issue You can call array_multisort() (or any other function) without specifying its arguments explicitly if you have the arguments in an array by using call_user_func_array(). array_multisort() gets its arguments by reference (in order to be able to modify them). You have to consider this when you build the array you use as argument to call_user_func_array() and put references in this array: // Input arrays
$ar[1] = array("game", "scissors", "rock", "paper");
$ar[2] = array("D", "B", "A", "C");
$ar[3] = array("four", "two", "one", "three");
// Build the list of arguments for array_multisort()
// Use references to the arrays you want it to sort
$supar = array(&$ar[2], &$ar[1], &$ar[3]);
// Call array_multisort() indirectly
// This is the same as array_multisort($ar[2], $ar[1], $ar[3]);
call_user_func_array('array_multisort', $supar);
|
Sorting array of arrays using array_multisort() [Argument #1 is expected to be an array or a sort flag]
Tag : php , By : Mare Astra
Date : March 29 2020, 07:55 AM
To fix this issue I think usort() can do the trick for you: $cats=[
["category_code"=>"BB","category"=>"","item_count"=>"1","max_qty"=>12000],
["category_code"=>"AK","category"=>"Anklet","item_count"=>"1","max_qty"=>6],
["category_code"=>"BAC","category"=>"Bag Accessories","item_count"=>"2","max_qty"=>352],
["category_code"=>"WB","category"=>"Bags","item_count"=>"9","max_qty"=>6290],
["category_code"=>"AB","category"=>"Bathroom Accessories","item_count"=>"19","max_qty"=>325],
["category_code"=>"BK","category"=>"Book","item_count"=>"40","max_qty"=>27291]
];
usort($cats,function($a,$b){
return $b['max_qty']-$a['max_qty'];
});
var_export($cats);
array (
0 =>
array (
'category_code' => 'BK',
'category' => 'Book',
'item_count' => '40',
'max_qty' => 27291,
),
1 =>
array (
'category_code' => 'BB',
'category' => '',
'item_count' => '1',
'max_qty' => 12000,
),
2 =>
array (
'category_code' => 'WB',
'category' => 'Bags',
'item_count' => '9',
'max_qty' => 6290,
),
3 =>
array (
'category_code' => 'BAC',
'category' => 'Bag Accessories',
'item_count' => '2',
'max_qty' => 352,
),
4 =>
array (
'category_code' => 'AB',
'category' => 'Bathroom Accessories',
'item_count' => '19',
'max_qty' => 325,
),
5 =>
array (
'category_code' => 'AK',
'category' => 'Anklet',
'item_count' => '1',
'max_qty' => 6,
),
)
|