I have an array as follows
Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[33636] => 1
[67376] => 2
[73250] => 1
[125313] => 2
[142062] => 1
[342520] => 2
[357301] => 2
[357303] => 1
[404419] => 1
[408957] => 1
[415891] => 2
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
My aim is to order them by the single digit value so the output would put the 2's(or highest number) to the top
Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[415891] => 2
[125313] => 2
[67376] => 2
[342520] => 2
[357301] => 2
[33636] => 1
[73250] => 1
[142062] => 1
[357303] => 1
[404419] => 1
[408957] => 1
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
Try with the arsort function:
arsort — Sort an array in reverse order and maintain index association
Example:
arsort($array);
// done, $array is now sorted
Built into PHP: http://php.net/manual/en/function.arsort.php
bascially you need toh known array function for php from here
and function as
arsort($arr);
Simple
arsort($array)
http://www.php.net/manual/en/function.arsort.php
Related
I'm ranking a list based on a set of tie-breaking criteria using usort in PHP. My problem is that the tiebreakers aren't applied correctly when there are 3 or more ties to break. The function only sorts between 2 entries as it iterates through the array.
When 2 entries have identical Overall wins, the function checks Division wins; if those are identical, it checks Head to head; then Points if the tie still is not broken. This works great to break the tie between 2 entries, but with 3 or more ties, the iterative nature of this process is an issue.
Example:
3 entries have identical Overall wins and Division wins, so we check Head to head results.
Team 1 beat Team 2.
Team 2 beat Team 3.
Team 3 beat Team 1.
Therefore, we should move on to Points because the tie cannot be broken with Head to head as each team has one win over the other two teams. But my function doesn't ever make that third check, I guess because Team A and Team C aren't next to each other when usort() does it's thing...I'm not sure.
Here is the comparison function I pass into usort:
function set_division_order($a, $b) {
if ($a['wins'] == $b['wins'] && $a['losses'] == $b['losses']) { //same overall record
if ($a['div_wins'] == $b['div_wins'] && $a['div_losses'] == $b['div_losses']) { //same division record
$h2h = get_h2h_result($year, $a['team_id'], $b['team_id']);
if ($h2h == 0) { //same head-to-head record
return ($b['pts'] - $a['pts']);
}
else { //head-to-head winner
return $h2h;
}
}
else { //different division record
return ($b['div_wins'] - $a['div_wins']);
}
}
else { //different overall record
return ($b['wins'] - $a['wins']);
}
}
The array getting passed into usort($array, 'set_division_order') which is causing problems:
Array
(
[0] => Array
(
[team_id] => 1
[wins] => 3
[losses] => 2
[ties] => 0
[div_wins] => 3
[div_losses] => 2
[div_ties] => 0
[pts] => 513.9
)
[1] => Array
(
[team_id] => 2
[wins] => 3
[losses] => 2
[ties] => 0
[div_wins] => 3
[div_losses] => 2
[div_ties] => 0
[pts] => 504.1
)
[2] => Array
(
[team_id] => 3
[wins] => 3
[losses] => 2
[ties] => 0
[div_wins] => 3
[div_losses] => 2
[div_ties] => 0
[pts] => 517.7
)
[3] => Array
(
[team_id] => 4
[wins] => 4
[losses] => 1
[ties] => 0
[div_wins] => 4
[div_losses] => 1
[div_ties] => 0
[pts] => 491.9
)
[4] => Array
(
[team_id] => 5
[wins] => 2
[losses] => 3
[ties] => 0
[div_wins] => 2
[div_losses] => 3
[div_ties] => 0
[pts] => 393.3
)
[5] => Array
(
[team_id] => 6
[wins] => 0
[losses] => 5
[ties] => 0
[div_wins] => 0
[div_losses] => 5
[div_ties] => 0
[pts] => 377.9
)
)
Sorry this was kind of long, but hopefully the problem is pretty evident. Now it's just time for a solution! I've scoured the PHP resources for the various sort functions, with no luck. I can't be the only one trying to break a 3-way tie with PHP. :)
If I can get just the tied teams matching that criteria into a separate array, I could use a function to re-sort the original sorted array against the values of this new sub-array to order the tied teams within the main array.
...And my head just exploded.
Thanks!
I have these tables
Loans Table
| coop_id | loan_id | loan_availability |
Loan Seasons Table
| coop_id | loan_id | loan_season_start | loan_season_end |
And I'm trying to get the details of each loan in one array whether their availability is Always or Seasonal. If it is marked as seasonal, it has a data in the loan seasons table.
This is my query
$loanlist = DB::table('loans')
->join('loan_seasons', 'loan_seasons.loan_id', 'loans.loan_id')
->where('loans.coop_id', $coop_id)
->where(function ($q){
$q->where('loans.loan_availability', "Always")
->orWhere(function ($qq){
$qq->where('loans.loan_availability', "Seasonal")
->where('loan_season_start', '>=', Carbon::now()->month)
->where('loan_season_end', '<=', Carbon::now()->month)
->where('loan_season_status', 1);
});
})
->where('loans.loan_status', "1")
->get()->toArray();
I'm getting this output:
Array(
[0] => Array(
[id] => 2
[coop_id] => 1
[loan_id] => 3
[loan_name] => Loan 1
[loan_desc] => Loan 1 Description
[loan_maxamount] => 500000
[loan_availability] => Always
[loan_status] => 1
[created_at] => 2017-02-26 17:43:08
[updated_at] => 2017-02-26 17:43:08
[loan_season_start] => 2
[loan_season_end] => 6
[loan_season_status] => 1
)
)
My expected output is like this:
Array(
[0] => Array(
[id] => 1
[coop_id] => 1
[loan_id] => 3
[loan_name] => Loan 1
[loan_desc] => Loan 1 Description
[loan_maxamount] => 500000
[loan_availability] => Always
[loan_status] => 1
[created_at] => 2017-02-26 17:43:08
[updated_at] => 2017-02-26 17:43:08
[loan_season_status] => 1
)
[1] => Array(
[id] => 2
[coop_id] => 1
[loan_id] => 4
[loan_name] => Loan 2
[loan_desc] => Loan 2 Description
[loan_maxamount] => 200000
[loan_availability] => Seasonal
[loan_season_start] => 2
[loan_season_end] => 6
[loan_status] => 1
[created_at] => 2017-02-26 17:43:08
[updated_at] => 2017-02-26 17:43:08
[loan_season_status] => 1
)
)
I've been trying to solve this for hours but I still can't get the query right. Can somebody help? I'm very new to nested queries. Help?
I got it working now. Seems like I've been making simple queries complicated. I created different queries for each condition and joined them into one instead. Thanks.
I'm on a Drupal7 site, and I'm not used to Drupal. When I edit a node (standard page), and try to save it, the menu disappears. Not all node's are like this, only the ones that uses a field group of heatmaps, probably a custom field group (legacy).
System specs are:
CentOS 6.6
Apache 2.2
Mysql 5.5
Php 7
At first, I thought it was a bug in Drupal 7, and I tried solutions as Menu items disappearing in Drupal 7. But the suggested solutions didn't work. So I started to suspect post_max_size or memory_limit, because the form with the custom field grows very large when it uses the field or the field group. So I've maxed the memory settings and it looks good but is still not working.
The field group array tends to be very large and I tried to find some info about nestling level too big in a post but found no hints.
The post size is:
post_max_size in bytes = 536870912
post CONTENT_LENGTH = 1020347
The field group contains Heatmaps with Geolocations and endless of data:
[field_heatmap_data] => Array
(
[und] => Array
(
[0] => Array
(
[tablefield] => Array
(
[cell_0_0] => X
[cell_0_1] => Y
[cell_0_2] => Plastic
[cell_0_3] => Paper
[cell_0_4] => Glass
[cell_0_5] => Metal
[cell_0_6] => Organiskt
[cell_0_7] =>
[cell_0_8] =>
[cell_0_9] => Other
[cell_1_0] => 14.1741233638
[cell_1_1] => 57.7797089972
[cell_1_2] => 0
[cell_1_3] => 0
[cell_1_4] =>
[cell_1_5] =>
[cell_1_6] =>
[cell_1_7] => 1
[cell_1_8] =>
[cell_1_9] => 2
[cell_2_0] => 14.1784435935
[cell_2_1] => 57.7797106709
[cell_2_2] => 0
[cell_2_3] => 0
[cell_2_4] =>
[cell_2_5] =>
[cell_2_6] =>
[cell_2_7] =>
[cell_2_8] =>
[cell_2_9] =>
[cell_3_0] => 14.1656472109
[cell_3_1] => 57.7831198751
[cell_3_2] => 1
[cell_3_3] => 2
[cell_3_4] => 1
[cell_3_5] => 1
[cell_3_6] =>
[cell_3_7] =>
[cell_3_8] =>
[cell_3_9] =>
[cell_4_0] => 14.1753179083
[cell_4_1] => 57.7826699822
[cell_4_2] => 0
[cell_4_3] => 5
[cell_4_4] =>
[cell_4_5] => 3
[cell_4_6] =>
[cell_4_7] => 9
[cell_4_8] => 4
[cell_4_9] =>
[cell_5_0] => 14.1602465906
[cell_5_1] => 57.7824661754
[cell_5_2] => 2
[cell_5_3] => 0
[cell_5_4] => 1
[cell_5_5] =>
[cell_5_6] =>
[cell_5_7] => 4
[cell_5_8] =>
[cell_5_9] => 1
[cell_6_0] => 14.1552312791
[cell_6_1] => 57.7788985858
[cell_6_2] => 0
[cell_6_3] => 1
[cell_6_4] =>
[cell_6_5] => 1
[cell_6_6] =>
[cell_6_7] => 4
[cell_6_8] =>
[cell_6_9] =>
[cell_7_0] => 14.1631063952
[cell_7_1] => 57.7813178687
[cell_7_2] => 1
[cell_7_3] => 0
[cell_7_4] =>
[cell_7_5] =>
[cell_7_6] =>
[cell_7_7] => 2
[cell_7_8] => 3
[cell_7_9] =>
[cell_8_0] => 14.1742044644
[cell_8_1] => 57.7827544419
[cell_8_2] => 0
[cell_8_3] => 0
[cell_8_4] =>
[cell_8_5] =>
[cell_8_6] =>
[cell_8_7] => 4
[cell_8_8] => 1
[cell_8_9] =>
[cell_9_0] => 14.157952438
[cell_9_1] => 57.7818974962
[cell_9_2] => 2
[cell_9_3] => 4
[cell_9_4] => 5
[cell_9_5] => 1
[cell_9_6] =>
[cell_9_7] => 8
[cell_9_8] => 2
[cell_9_9] =>
[cell_10_0] => 14.1706946744
[cell_10_1] => 57.7815507326
[cell_10_2] => 0
[cell_10_3] => 0
[cell_10_4] =>
And so on....
So I've figured out that there is a flaw in the architecture of the node because it cant clearly not handle that much data in a field group and should been handled as an separate node, but since this is a legacy project I don't want to mess things up.
If I var_dump the $_POST variable on different pages when editing, I can clearly see that the $_POST variable stops after the $_POST['field_heatmap'] element where there is data, while the pages that doesn't contain data in that field group the $_POST array continues after the $_POST['field_heatmap'] element.
So my question is, should I continue to try to find a bug in Drupal or should I investigate further some php settings (Or maybe apache). I've tried debugging with cachegrind but can't find any unusual. Or any hints are greatly appreciated!
Finally! The max_input_vars was set to 1000
Changed it to max_input_vars = 10000 and it worked!
I have a multidimensional array like this:
Array (
[0] => Array ( [ans_id] => 1 [ans_name] => PHP Hypertext Preprocessor [ques_id] => 1 [right_ans] => Yes [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[1] => Array ( [ans_id] => 2 [ans_name] => Preety Home Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[2] => Array ( [ans_id] => 3 [ans_name] => Programmed Hypertext Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[3] => Array ( [ans_id] => 4 [ans_name] => Programmed Hypertext Preprocessor [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) )
My table is like this:
ans_id ans_name ques_id right_ans
1 PHP Hypertext Preprocessor 1 Yes
2 Preety Home Page 1 No
3 Programmed Hypertext Page 1 No
4 Programmed Hypertext Preprocessor 1 No
5 Andy Suraski 12 No
6 Zeev Gutman 12 No
7 Rasmus Lerdorf 12 Yes
8 Perl 12 No
I want to retrieve the answers in sets of common question id's
i.e. one set of all answers for Ques_id='1', 2nd set of all answers for ques_id='12', etc.
I am supposed to use this code:
foreach($all_ans_cat as $r)
{
echo $r['ans_id']." ".$r['ans_name']."<br>";
}
This is retrieving all the values present in the table as one set and again in the 2nd set the same values are being displayed.
Select all answers sorted by ques_id and generate array with structure needed
$questions = array();
$prevQuestionId = null;
foreach ($all_ans_cat as $r) {
if ($r['ques_id'] !== $prevQuestionId) {
$questions[] = array();
}
$questions[sizeof($questions)-1][] = $r;
$prevQuestionId = $r['ques_id'];
}
Result array would be:
0:
- [ans1, ques1]
- [ans2, ques1]
...
1:
- [ans5, ques13]
- [ans6, ques13]
...
...
you can use a query with group by and aggregate function
have a look at
http://www.w3schools.com/sql/sql_groupby.asp
I have the following output of an array using PHP. I need to do two things... First, I need to sort the array so it prints by the most recent date. I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ...
Then I need to count how many rows exist for each year.
So, in the example below, I would need to know that there are ...
2 entries from 2010
2 entries from 2011
1 entry from 2012
Stop counting when there are no more rows
Since the year is not separate from the rest of the date digits, this also complicates things...
Array
(
[0] => Array
(
[racer_date] => 11/15/2010
[racer_race] => Test Row 4
[racer_event] => 321
[racer_time] => 16
[racer_place] => 12
[racer_medal] => 1
)
[1] => Array
(
[racer_date] => 7/15/2010
[racer_race] => Test Row 3
[racer_event] => 123
[racer_time] => 14
[racer_place] => 6
[racer_medal] => 0
)
[2] => Array
(
[racer_date] => 7/28/2011
[racer_race] => Test Row
[racer_event] => 123
[racer_time] => 10
[racer_place] => 2
[racer_medal] => 2
)
[3] => Array
(
[racer_date] => 10/9/2011
[racer_race] => Test Row 2
[racer_event] => 321
[racer_time] => 12
[racer_place] => 3
[racer_medal] => 3
)
[4] => Array
(
[racer_date] => 10/3/2012
[racer_race] => World Indoor Championships (final)
[racer_event] => 400m
[racer_time] => 50.79
[racer_place] => 1
[racer_medal] => 1
)
)
function cmp($a, $b)
{
if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
return 0;
}
return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}
usort($array, "cmp");
call your array $array, and above code will sort it..
And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit..