This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
How to compare 2 strings alphabetically in PHP?
(5 answers)
Closed 5 years ago.
I have an array called "$myarray", which looks like this:
[0] => Array
(
[PrinterID] => 4
[PrinterName] => PRT04_GL
[UserID] => 1
[isDefaultPrinter] => 0
[isMapped] => 0
)
[1] => Array
(
[PrinterID] => 1
[PrinterName] => PRT01_Zentral
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[2] => Array
(
[PrinterID] => 2
[PrinterName] => PRT02_BH
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[3] => Array
(
[PrinterID] => 3
[PrinterName] => PRT03_EDV
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 1
)
I want to sort the data by ["PrinterName"]. The wanted result would be:
[0] => Array
(
[PrinterID] => 1
[PrinterName] => PRT01_Zentral
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[1] => Array
(
[PrinterID] => 2
[PrinterName] => PRT02_BH
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[2] => Array
(
[PrinterID] => 3
[PrinterName] => PRT03_EDV
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 1
)
[3] => Array
(
[PrinterID] => 4
[PrinterName] => PRT04_GL
[UserID] => 1
[isDefaultPrinter] => 0
[isMapped] => 0
)
Based on this example I tried the following:
uasort($myarray, 'sort_by_order');
function sort_by_order ($a, $b) {
return $a['PrinterName'] - $b['PrinterName'];
}
This does not work. I get errors like "A non-numeric value encountered ...", of course because my values are strings and the function is for numeric values.
How do I sort the array?
Thank you!
EDIT:
I think I did find the solution here. I used this and it seems to work:
uasort($myarray, 'sort_by_order');
function sort_by_order ( $a, $b ) { return strcmp($a['PrinterName'], $b['PrinterName']); }
Please try with $a['PrinterName'] > $b['PrinterName']
It will compare the values
Related
I have collection of array and i want delete array of array where "answer_id" key is not exits.
my array look like this.
Array
(
[0] => Array
(
[question_no] => 1
[subject_id] => 1
[question_id] => 255
[currect_ans_id] => 2657
[time_taken] => 110
[is_visited] => 1
[is_saved] => 0
[answer_id] => 2659
)
[1] => Array
(
[question_no] => 2
[subject_id] => 1
[question_id] => 256
[currect_ans_id] => 2662
[time_taken] => 0
[is_visited] => 1
[is_saved] => 0
)
)
and want array like this(where answer_id key exits).
Array
(
[0] => Array
(
[question_no] => 1
[subject_id] => 1
[question_id] => 255
[currect_ans_id] => 2657
[time_taken] => 110
[is_visited] => 1
[is_saved] => 0
[answer_id] => 2659
)
)
You can use array_filter to remove entries which don't have an answer_id:
$output = array_filter($input, function ($a) { return isset($a['answer_id']); });
Demo on 3v4l.org
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
How can we sort an associative array with custom order?
My array look like this
Array
(
[pa_color] => Array
(
[name] => pa_color
[value] =>
[position] => 0
[is_visible] => 1
[is_variation] => 1
[is_taxonomy] => 1
)
[pa_dimension] => Array
(
[name] => pa_dimension
[value] =>
[position] => 1
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travel-duration] => Array
(
[name] => pa_travel-duration
[value] =>
[position] => 2
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travel-type] => Array
(
[name] => pa_travel-type
[value] =>
[position] => 3
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travelling-with] => Array
(
[name] => pa_travelling-with
[value] =>
[position] => 4
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_volume] => Array
(
[name] => pa_volume
[value] =>
[position] => 5
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_weight] => Array
(
[name] => pa_weight
[value] =>
[position] => 6
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
)
and i want this array is like pa_travel-duration first pa_volume second ?? I know there is a native php function usort but i could not understand this.
This will do the work, but im pretty sure there's much better ways to do it:
Code:
$array = array("pa_color" => "color",
"pa_dimension" => "dimension",
"pa_travel-duration" => "Random Stuff: " . rand(100,999),
"pa_volume" => "volumen"
);
$tmp = array("pa_travel-duration" => $array["pa_travel-duration"],
"pa_volume" => $array["pa_volume"],
);
unset($array["pa_travel-duration"], $array["pa_volume"]);
$array = array_merge($tmp,$array);
print_r($array);
Result:
Array
(
[pa_travel-duration] => Random Stuff: 127
[pa_volume] => volumen
[pa_color] => color
[pa_dimension] => dimension
)
Take care because if the array doesn't have the proper key it will throw an error, you need to add few checks there.
$sort_by = array('pa_travel-duration', 'pa_volume', 'pa_color','pa_dimension','pa_travel-type','pa_travelling-with','pa_weight');
$temp_arr = array();
foreach ($sort_by as $key) {
$temp_arr[$key] = $data[$key];
}
$data = $temp_arr;
echo '<pre>'; print_r($data);
Define your order in $sort_by array
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
How can I sort arrays and data in PHP?
(14 answers)
Closed 6 years ago.
I have this array :
Array
(
[26] => Array
(
[total_auctions] => 1
[total_price] => 0
)
[24] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[25] => Array
(
[total_auctions] => 0
[total_price] => 0
)
)
I want to sort this array to be :
Array
(
[24] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[25] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[26] => Array
(
[total_auctions] => 1
[total_price] => 0
)
)
I tried with array_multisort but not work. Can you help me please ? Thx in advance. I don't understand where is the problem, normally should work
Just use ksort() to sort any array keys
<?php
$arr = array(
'26' => array('total_auctions' => 1,'total_price' => 0),
'24' => array('total_auctions' => 0,'total_price' => 0),
'25' => array('total_auctions' => 0,'total_price' => 0)
);
ksort($arr);
print '<pre>';print_r($arr);
exit;
?>
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I know there are some other topics about sorting with multiple criteria, but they don't fix my problem.
Let's say I have this array:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
I want to sort it, putting the one with the HIGHEST score on top. On same score, I want the one with the LOWEST endgame number on top.
The sorting mechanisme should rank user1 on top, then user2, then 4 and then user3.
I use this sorting mechanisme:
function order_by_score_endgame($a, $b)
{
if ($a['score'] == $b['score'])
{
// score is the same, sort by endgame
if ($a['endgame'] == $b['endgame']) return 0;
return $a['endgame'] == 'y' ? -1 : 1;
}
// sort the higher score first:
return $a['score'] < $b['score'] ? 1 : -1;
}
usort($dummy, "order_by_score_endgame");
This gives me the following array:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[2] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
As you can see, the array isn't sorted properly... Anyone knows what I'm doing wrong? Thanks a lot!
Your function should be like this:
function order_by_score_endgame($a, $b) {
if ($a['score'] == $b['score']) {
// score is the same, sort by endgame
if ($a['endgame'] > $b['endgame']) {
return 1;
}
}
// sort the higher score first:
return $a['score'] < $b['score'] ? 1 : -1;
}
Try it out. It will give you result like this:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
[3] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sorting array based on inner-array key-value
Array (
[0] => Array (
[id] => 2
[name] => 4
[closed] =>
[date] => 1319625994
[votes_up] => 0
[votes_down] => 0
[votes_pct_up] => 0
[votes_pct_down] => 0
[votes_balance] => 0
[votes_total] => 0 )
[1] => Array (
[id] => 3
[name] => 3
[closed] =>
[date] => 1319625994
[votes_up] => 0
[votes_down] => 0
[votes_pct_up] => 0
[votes_pct_down] => 0
[votes_balance] => 0
[votes_total] => 0 )
[2] => Array (
[id] => 4
[name] => 2
[closed] =>
[date] => 1319625995
[votes_up] => 1
[votes_down] => 0
[votes_pct_up] => 100
[votes_pct_down] => 0
[votes_balance] => 1
[votes_total] => 1 )
[3] => Array (
[id] => 1
[name] => 1
[closed] =>
[date] => 1319623450
[votes_up] => 2
[votes_down] => 0
[votes_pct_up] => 100
[votes_pct_down] => 0
[votes_balance] => 2
[votes_total] => 2 )
)
How do i sort these according to their [votes_balance] value?
Use the usort function of PHP with something like this:
function compare($a, $b) {
if($a['id'] == $b['id']) return 0;
return ($a['id'] < $b['id']) -1 : 1;
}