I may have stupid question but I do not know how to solve my problem.
What I want to achieve.
I need to divide the field by date (key) (specifically, I'm interested in the year) and figure out the total amount (value) for each year.
my array:
Array
(
[2017-04-20 15:26:08] => 0.00
[2016-10-11 13:52:05] => 0.00
[2016-05-26 13:04:14] => 200000.00
[2016-05-24 14:32:34] => 0.00
[2016-05-24 11:32:17] => 0.00
[2016-03-29 19:02:24] => 79800.00
[2016-02-16 07:54:38] => 0.00
[2015-12-16 19:01:55] => 149900.00
[2015-01-30 15:07:38] => 402103.00
[2014-11-03 15:13:29] => 0.00
[2014-10-15 15:58:44] => 129000.00
[2014-10-06 12:44:52] => 0.00
[2014-07-28 10:47:45] => 0.00
[2014-07-23 15:50:24] => 133333.33
[2014-05-16 12:39:25] => 0.00
[2014-04-29 13:21:01] => 5045524.00
[2014-03-04 15:28:03] => 0.00
[2014-01-13 09:41:47] => 0.00
[2013-11-26 08:40:20] => 70000.00
[2013-07-22 12:17:24] => 0.00
[2012-10-05 13:51:06] => 0.00
[2012-03-08 15:45:44] => 149880.00
[2012-03-02 13:11:19] => 0.00
[2012-02-14 09:26:43] => 0.00
[2011-12-05 10:44:23] => 0.00
[2011-12-05 10:33:53] => 1500000.00
[2011-07-01 14:40:22] => 0.00
[2011-05-30 09:54:22] => 1680000.00
[2011-01-13 08:59:14] => 72000.00
)
my code:
$min_year = date('Y',strtotime(min(array_column($MY_ARRAY,'published_at'))));
$max_year = date('Y',strtotime(max(array_column($MY_ARRAY,'published_at'))));
for($year = $max_year; $year >= $min_year; $year--){
$amount_array = array_column($MY_ARRAY,'contract_amount','published_at');
echo 'Year: ' .$year . ', Total amount for this year: ' . ???? . ' €';
}
how should I do it?
edit// part of original array
Array
(
[0] => stdClass Object
(
[id] => 2898917
[contract_identifier] => 155/AD00/2017
[contracting_authority_name] => some company
[contracting_authority_formatted_address] => address
[contracting_authority_cin] => 123456789
[supplier_name] => another company
[supplier_formatted_address] =>
[supplier_cin] => n123456789mber
[subject] => contract title
[subject_description] =>
[signed_on] => 2017-04-19
[effective_note] =>
[contract_price_amount] => 0.00
[contract_price_total_amount] => 0.00
[note] =>
[published_at] => 2017-04-20 15:26:08
[changed_at] => 2017-04-20 15:10:09
[change_note] =>
[internal_id] => 155
[internal_note] =>
[confirmation_file_name] =>
[confirmed_on] =>
[source_id] => 2
[description] =>
[reference] =>
[effective_from] => 2017-04-21
[effective_to] =>
[crz_department_name] => department
[crz_status_name] => 1
[crz_type_name] => 1
[crz_kind_name] => 2
[crz_confirmation_status_name] => 5
[crz_attachments_title] => 11111
[crz_attachments_file_name] =>
[crz_attachments_file_size] =>
[crz_attachments_scan_file_name] => 2898918_dokument.pdf
[crz_attachments_scan_file_size] => 373463
)
[1] => stdClass Object
(
[id] => 2635819
[contract_identifier] => 241
[contracting_authority_name] => ...
...
...
I would do something like this:
Loop through the whole stuff.
Create another array.
Get the date part and parse the year.
Cumulatively add the years as the key and value as the sum.
Display.
Code:
<?php
$arr = array(
"2017-04-20 15:26:08" => 0.00,
"2016-10-11 13:52:05" => 0.00,
"2016-05-26 13:04:14" => 200000.00,
"2016-05-24 14:32:34" => 0.00,
"2016-05-24 11:32:17" => 0.00,
"2016-03-29 19:02:24" => 79800.00,
"2016-02-16 07:54:38" => 0.00,
"2015-12-16 19:01:55" => 149900.00,
"2015-01-30 15:07:38" => 402103.00,
"2014-11-03 15:13:29" => 0.00,
"2014-10-15 15:58:44" => 129000.00,
"2014-10-06 12:44:52" => 0.00,
"2014-07-28 10:47:45" => 0.00,
"2014-07-23 15:50:24" => 133333.33,
"2014-05-16 12:39:25" => 0.00,
"2014-04-29 13:21:01" => 5045524.00,
"2014-03-04 15:28:03" => 0.00,
"2014-01-13 09:41:47" => 0.00,
"2013-11-26 08:40:20" => 70000.00,
"2013-07-22 12:17:24" => 0.00,
"2012-10-05 13:51:06" => 0.00,
"2012-03-08 15:45:44" => 149880.00,
"2012-03-02 13:11:19" => 0.00,
"2012-02-14 09:26:43" => 0.00,
"2011-12-05 10:44:23" => 0.00,
"2011-12-05 10:33:53" => 1500000.00,
"2011-07-01 14:40:22" => 0.00,
"2011-05-30 09:54:22" => 1680000.00,
"2011-01-13 08:59:14" => 72000.00
);
$sum = array();
foreach ($arr as $year => $value) {
$year = date('Y', strtotime($year));
if (!isset($sum[$year]))
$sum[$year] = 0.0;
$sum[$year] += $value;
}
?>
Output
array(7) {
[2017]=>
float(0)
[2016]=>
float(279800)
[2015]=>
float(552003)
[2014]=>
float(5307857.33)
[2013]=>
float(70000)
[2012]=>
float(149880)
[2011]=>
float(3252000)
}
Would be way more easier to do something like:
$sum = array();
foreach ($MY_ARRAY as $date => $value) {
$year = substr($date, 0, 4);
$sum[$year] = (isset($sum[$year]) ? $sum[$year] : 0) + $value;
}
print_r($sum);
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!
so I'm working on a tool for fantasy baseball. I constructed this SQL statement that I thought worked initially but turned out to not do so after I added more rows.
Here is the statement:
SELECT
p.player_id, position, last_name, first_name, player_type, team, s.season_gp, s.season_runs, s.season_hits, s.season_doubles, s.season_triples, s.season_home_runs, s.season_runs_batted_in, s.season_base_on_balls, s.season_strikeouts, s.season_stolen_bases, w.week_gp, w.week_runs, w.week_hits, w.week_doubles, w.week_triples, w.week_home_runs, w.week_runs_batted_in, w.week_base_on_balls, w.week_strikeouts, w.week_stolen_bases
FROM
player AS p
LEFT JOIN
(SELECT
player_id, COUNT(date) AS week_gp, SUM(runs) AS week_runs, SUM(hits) AS week_hits, SUM(doubles) AS week_doubles, SUM(triples) AS week_triples, SUM(home_runs) AS week_home_runs, SUM(runs_batted_in) AS week_runs_batted_in, SUM(base_on_balls) AS week_base_on_balls, SUM(strikeouts) AS week_strikeouts, SUM(stolen_bases) AS week_stolen_bases
FROM
game_mlb
WHERE
date >= CURDATE() - INTERVAL 1 WEEK) w
ON p.player_id = w.player_id
LEFT JOIN
(SELECT
player_id, COUNT(date) AS season_gp, SUM(runs) AS season_runs, SUM(hits) AS season_hits, SUM(doubles) AS season_doubles, SUM(triples) AS season_triples, SUM(home_runs) AS season_home_runs, SUM(runs_batted_in) AS season_runs_batted_in, SUM(base_on_balls) AS season_base_on_balls, SUM(strikeouts) AS season_strikeouts, SUM(stolen_bases) AS season_stolen_bases
FROM
game_mlb
WHERE
date >= YEAR(CURDATE())) s
ON p.player_id = s.player_id
WHERE
team_id = 1
Here are the PHP arrays for the tables being used.
game_mlb:
$game_mlb = array(
array('game_id' => '9','player_id' => '1','date' => '2013-03-31','at_bats' => '4','runs' => '0','hits' => '0','doubles' => '0','triples' => '0','home_runs' => '0','runs_batted_in' => '0','stolen_bases' => '0','base_on_balls' => '0','strikeouts' => '0','innings_pitched' => NULL,'wins' => NULL,'complete_games' => NULL,'shutouts' => NULL,'saves' => NULL,'earned_runs' => NULL,'strikeouts_pitched' => NULL,'holds' => NULL),
array('game_id' => '10','player_id' => '2','date' => '2013-04-01','at_bats' => '4','runs' => '0','hits' => '0','doubles' => '0','triples' => '0','home_runs' => '0','runs_batted_in' => '0','stolen_bases' => '0','base_on_balls' => '1','strikeouts' => '1','innings_pitched' => NULL,'wins' => NULL,'complete_games' => NULL,'shutouts' => NULL,'saves' => NULL,'earned_runs' => NULL,'strikeouts_pitched' => NULL,'holds' => NULL)
);
player:
$player = array(
array('player_id' => '1','player_league' => 'MLB','first_name' => 'Adrian','last_name' => 'Beltre','player_type' => 'MLB_BATTER','position' => '3B','team' => 'TEX','yahoo_id' => '6039','minors_type' => 'MLB_BR','minors_id' => 'beltre001adr','injury' => NULL,'team_id' => '1'),
array('player_id' => '2','player_league' => 'MLB','first_name' => 'Albert','last_name' => 'Pujols','player_type' => 'MLB_BATTER','position' => '1B','team' => 'LAA','yahoo_id' => '6619','minors_type' => 'MLB_BR','minors_id' => 'pujols001jos','injury' => NULL,'team_id' => '1')
);
And here is the result I'm getting:
$player = array(
array('player_id' => '1','position' => '3B','last_name' => 'Beltre','first_name' => 'Adrian','player_type' => 'MLB_BATTER','team' => 'TEX','season_gp' => '2','season_runs' => '0','season_hits' => '0','season_doubles' => '0','season_triples' => '0','season_home_runs' => '0','season_runs_batted_in' => '0','season_base_on_balls' => '1','season_strikeouts' => '1','season_stolen_bases' => '0','week_gp' => '2','week_runs' => '0','week_hits' => '0','week_doubles' => '0','week_triples' => '0','week_home_runs' => '0','week_runs_batted_in' => '0','week_base_on_balls' => '1','week_strikeouts' => '1','week_stolen_bases' => '0'),
array('player_id' => '2','position' => '1B','last_name' => 'Pujols','first_name' => 'Albert','player_type' => 'MLB_BATTER','team' => 'LAA','season_gp' => NULL,'season_runs' => NULL,'season_hits' => NULL,'season_doubles' => NULL,'season_triples' => NULL,'season_home_runs' => NULL,'season_runs_batted_in' => NULL,'season_base_on_balls' => NULL,'season_strikeouts' => NULL,'season_stolen_bases' => NULL,'week_gp' => NULL,'week_runs' => NULL,'week_hits' => NULL,'week_doubles' => NULL,'week_triples' => NULL,'week_home_runs' => NULL,'week_runs_batted_in' => NULL,'week_base_on_balls' => NULL,'week_strikeouts' => NULL,'week_stolen_bases' => NULL)
);
This is incorrect. Beltre and Pujols should each have 1 game played with their respective stats, but Beltre is getting credited with everything.
Any help would be appreciated, I've been stuck on this for a while now.
You need group by statements in your subquery:
SELECT p.player_id, position, last_name, first_name, player_type, team,
s.season_gp, s.season_runs, s.season_hits, s.season_doubles, s.season_triples, s.season_home_runs, s.season_runs_batted_in, s.season_base_on_balls, s.season_strikeouts, s.season_stolen_bases, w.week_gp, w.week_runs, w.week_hits, w.week_doubles, w.week_triples, w.week_home_runs, w.week_runs_batted_in, w.week_base_on_balls, w.week_strikeouts, w.week_stolen_bases
FROM player p LEFT JOIN
(SELECT player_id, COUNT(date) AS week_gp, SUM(runs) AS week_runs, SUM(hits) AS week_hits, SUM(doubles) AS week_doubles, SUM(triples) AS week_triples, SUM(home_runs) AS week_home_runs, SUM(runs_batted_in) AS week_runs_batted_in, SUM(base_on_balls) AS week_base_on_balls, SUM(strikeouts) AS week_strikeouts, SUM(stolen_bases) AS week_stolen_bases
FROM game_mlb
WHERE date >= CURDATE() - INTERVAL 1 WEEK
group by player_id
) w
ON p.player_id = w.player_id LEFT JOIN
(SELECT player_id, COUNT(date) AS season_gp, SUM(runs) AS season_runs, SUM(hits) AS season_hits, SUM(doubles) AS season_doubles, SUM(triples) AS season_triples, SUM(home_runs) AS season_home_runs, SUM(runs_batted_in) AS season_runs_batted_in, SUM(base_on_balls) AS season_base_on_balls, SUM(strikeouts) AS season_strikeouts, SUM(stolen_bases) AS season_stolen_bases
FROM game_mlb
WHERE date >= YEAR(CURDATE())
group by player_id
) s
ON p.player_id = s.player_id
WHERE team_id = 1;
In most versions of SQL, your code would have returned an error. However, MySQL allows columns in the select list that are not in the group by -- and this has caused your problem.