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
Related
I have an array that looks like this:
Array
(
[0] => stdClass Object
(
[quiz_id] => 1033
[quiz_venue] => 6
[quiz_host] => 46
[quiz_golden_question] => 100
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 100
[quiz_trainee] => 0
)
[1] => stdClass Object
(
[quiz_id] => 985
[quiz_venue] => 57
[quiz_host] => 21
[quiz_golden_question] => 0
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 310
[quiz_trainee] => 0
)
I want to go through each array, and insert a new value (quiz_venue_name), this is what I have;
$quizzes = $wpdb->get_results( $prepared );
foreach ($quizzes as $quiz => $item) {
$venuetitle = get_the_title($item->quiz_venue);
$quizzes['quiz_venue_name'] = $venuetitle;
}
return $quizzes;
However, all it does is add the new values to the very end of the multidimensional array as new arrays - rather than adding them into each!
I feel like I'm doing something obvious wrong, so any help is much appreciated!
The end result I need would be;
Array
(
[0] => stdClass Object
(
[quiz_id] => 1033
[quiz_venue] => 6
[quiz_host] => 46
[quiz_golden_question] => 100
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 100
[quiz_trainee] => 0
[quiz_venue_name] => NEW VALUE
)
[1] => stdClass Object
(
[quiz_id] => 985
[quiz_venue] => 57
[quiz_host] => 21
[quiz_golden_question] => 0
[quiz_golden_question_outcome] => 0
[quiz_running] => 1
[quiz_status] => 310
[quiz_trainee] => 0
[quiz_venue_name] => NEW VALUE
)
Your $quizzes variable is an array of objects (Instance of stdClass), so you should use it attribute to set value in each iteration ($item->quiz_venue_name instead of $quizzes['quiz_venue_name']):
...
foreach ($quizzes as $quiz => $item) {
$venuetitle = get_the_title($item->quiz_venue);
$item->quiz_venue_name = $venuetitle;
}
...
In fact the $quizzes['quiz_venue_name']=... code will be set a value for index quiz_venue_name in root of $quizzes array.
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
I have used below query to get array result with id as key value,
$this->db->select('gp.id, gp.lot_no, SUM(gb.weight) AS weight, SUM(gb.staple) AS staple, SUM(gb.mic) AS mic, SUM(gb.strength) AS strength, SUM(gb.trash) AS trash, gb.color_grade');
$this->db->from(GIN_BALES . ' gb');
$this->db->join(GIN_PROCESS . ' gp', 'gp.id=gb.process_id');
$this->db->where('gb.sold_status', 0);
$this->db->where('gp.ginner_id', $this->prscr_id);
$this->db->where('gp.program', $program_id);
$this->db->group_by('gp.id');
$lot_details = $this->db->get()->result();
But I getting the array result like below and array key have 0,1.I want that array key with id like,561,562.
Array
(
[0] => stdClass Object
(
[id] => 561
[lot_no] => 1
[weight] => 16230
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
[1] => stdClass Object
(
[id] => 562
[lot_no] => 2
[weight] => 15523
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
)
Can anyone provide solution for this query issue?
Here is one liner,
// null will take id as key and whole array as value
$temp = array_column($temp, null, "id");
Reference: array_column.
Demo.
Output:
Array
(
[561] => stdClass Object
(
[id] => 561
[lot_no] => 1
[weight] => 16230
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
[562] => stdClass Object
(
[id] => 562
[lot_no] => 2
[weight] => 15523
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
)
I am getting this result after performing the following on an array. Shouldn't this be sorting by 'computer_name'? You will see they are not alpha order.
usort($test, function($a, $b) {
return $a['computer_name'] - $b['computer_name'];
});
echo "<pre>";
print_r($test);
echo "</pre>";
Array
(
[0] => Array
(
[computer_id] => 367
[account_id] => 1
[computer_name] => EXAMPLE_ONE
[last_username] => muah
[timestamp] => 2014-01-01 17:06:04
)
[1] => Array
(
[computer_id] => 366
[account_id] => 1
[computer_name] => TESTING
[last_username] =>
[timestamp] => 2013-12-06 20:02:14
)
[2] => Array
(
[computer_id] => 365
[account_id] => 1
[computer_name] => JOE-SCHMOE
[last_username] =>
[timestamp] => 2013-12-06 20:02:03
)
[3] => Array
(
[computer_id] => 18
[account_id] => 1
[computer_name] => SPORT-ONE
[last_username] =>
[timestamp] => 2012-10-16 03:31:22
)
[4] => Array
(
[computer_id] => 3
[account_id] => 1
[computer_name] => SPORT-TWO
[last_username] =>
[timestamp] => 2011-03-03 03:35:46
)
)
I think the issue is that EXAMPLE_ONE - TESTING, or EXAMPLE_ONE - SPORT-ONE is always equal to 0.
You could try using strcasecmp here, or the case insensitive version strcmp
Actually, looking at the documentation for usort, there's an example right in there. Your example above would be something like
usort($test, function($a, $b) {
return strcmp($a['computer_name'], $b['computer_name']);
});
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;
}