Sort an array based on its sub-array [duplicate] - php

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

Related

PHP - Sort Multidimensional Array by string [duplicate]

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

Sort an associative array with custom order [duplicate]

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

PHP, Sort Multidimensional Array by Child Array

I need to sort my multidimensional array by a value in the child array. In the array example below, I need to sort the parent arrays by child value "revenue_certificate".
function custom_sort($a, $b) {
return strcmp($a['revenue_certificate'], $b['revenue_certificate']);
}
usort($data_array, 'custom_sort');
I feel like I'm almost there, but where I simply don't understand is how to reference the child array value of "revenue_certificate".
Array
(
[0] => Array
(
[company_id] => 130
[company_name] => Eureka / Brookings
[revenue_certificate] => 3
[revenue_cash] => 33
[average_sale] => 0
[total_orders] => 0
[certificates_per_order] => -1
[revenue_per_certificate] => -1
[visible_items] => 25
[retail_value] => -1
[average_discount] => -1
[new_advertisers] => 1
[emails_harvested] => 1
[new_customers] => 1
)
[1] => Array
(
[company_id] => 82
[company_name] => Big Deals Across America
[revenue_certificate] => 1
[revenue_cash] => 0
[average_sale] => 0
[total_orders] => 0
[certificates_per_order] => -1
[revenue_per_certificate] => -1
[visible_items] => 1
[retail_value] => -1
[average_discount] => -1
[new_advertisers] => 0
[emails_harvested] => 0
[new_customers] => 0
)
[2] => Array
(
[company_id] => 134
[company_name] => Fergus Falls, MN
[revenue_certificate] => 2
[revenue_cash] => 20
[average_sale] => 0
[total_orders] => 0
[certificates_per_order] => -1
[revenue_per_certificate] => -1
[visible_items] => 128
[retail_value] => -1
[average_discount] => -1
[new_advertisers] => 129
[emails_harvested] => 2
[new_customers] => 1
)
)
Thanks for any help.
Don't use strcmp :)
function custom_sort($a, $b) {
return $a['revenue_certificate'] - $b['revenue_certificate'];
}
usort($data_array, 'custom_sort');
custom_sort should return a negative, 0, positive value when $a < $b, $a == $b, $a < $b respectively (just as strcmp does BTW).
I believe you need to use ksort instead
Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.
Array ( [0] => Array ( [India] => 2 ) [1] => Array ( [Canada] => 1 ) [2] => Array ( [USA] => 3 ) )
also sort this desc order according to country value

Sort array using multiple criteria in PHP [duplicate]

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

How to compare array keys' values and insert new array elements into first array?

I've two arrays as follows:
$grid_data = Array
(
[0] => Array
(
[newsletter_id] => 1
[newsletter_name] => Eywa Solutions
[newsletter_subject] => Holi Wishes
[newsletter_email_body] => Happy Holi to all the friends
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 17 2014, 16:21 pm
[newsletter_updated_date] => 0
)
[1] => Array
(
[newsletter_id] => 2
[newsletter_name] => Akshay Nikte
[newsletter_subject] => The Don
[newsletter_email_body] => How are yoy Nikte Saheb?
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 18 2014, 06:52 am
[newsletter_updated_date] => 0
)
)
and second array is as follows:
$LastSendNewsletterDetail = Array
(
[0] => Array
(
[newsletter_id] => 1
[newsletter_sent_count] => 5
[newsletter_sent_date] => 1395121193
)
[1] => Array
(
[newsletter_id] => 2
[newsletter_sent_count] => 7
[newsletter_sent_date] => 1395121227
)
)
Now what I want to achieve is compare the key values(named newsletter_id) with each other from above two arrays. If they match with each other then insert the data from array $LastSendNewsletterDetail to the first array $grid_data. If they don't match then insert blank values to the first array $grid_data.
For example the desired ultimate $grid_data array should look like this :
$grid_data = Array
(
[0] => Array
(
[newsletter_id] => 1
[newsletter_name] => Eywa Solutions
[newsletter_subject] => Holi Wishes
[newsletter_email_body] => Happy Holi to all the friends
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 17 2014, 16:21 pm
[newsletter_updated_date] => 0
[newsletter_sent_count] => 5
[newsletter_sent_date] => 1395121193
)
[1] => Array
(
[newsletter_id] => 2
[newsletter_name] => Akshay Nikte
[newsletter_subject] => The Don
[newsletter_email_body] => How are yoy Nikte Saheb?
[newsletter_call_to_action_status] => 0
[newsletter_call_to_action_text] =>
[newsletter_call_to_action_link] =>
[newsletter_status] => 2
[newsletter_schedule] => 0
[newsletter_created_date] => Mar 18 2014, 06:52 am
[newsletter_updated_date] => 0
[newsletter_sent_count] => 7
[newsletter_sent_date] => 1395121227
)
)
How to achieve this? Thanks in advance.
try this
for($i=0; $i < $grid_data.lenght; $i++)
{
$track =0;
foreach($LastSendNewsletterDetail as $value)
{
if($value[newsletter_id] === $grid_data[$i][newsletter_id] )
{
array_merge($grid_data[$i], $value);
$track=1;
}
}
if($track === 0)
{
//insert blank value here
}
}

Categories