Include month if not there in php array - php

I have an array like this in php
Array
(
[0] => Array
(
[month] => April-2014
[total_booking] => 2
)
[1] => Array
(
[month] => May-2014
[total_booking] => 5
)
[2] => Array
(
[month] => June-2014
[total_booking] => 25
)
[3] => Array
(
[month] => October-2013
[total_booking] => 1
)
[4] => Array
(
[month] => July-2014
[total_booking] => 4
)
)
I have to make this array
if i selected two months from_month , to_month if there are no any months in this array it should be included eg:
i have selected from_month 2014 feb to_month 2014 may . but if in my array only 2014 feb, 2014 april,2014 may only so iwat to include 2014 march in correct place.
like this
Array
(
[month] => March-2014
[total_booking] => 0
)
this is my code
foreach ($newarray as $month => total_booking) {
//sorting
}
foreach ($newarray as $month => total_booking) {
//if there is no month in array betwean to_month and from_month it
should be included in correct place as sorted
}

The best way is to take reference from the start date and end date you inputted and look it up in the master array..
Here it is(please scroll for answer)
$your_array=array
(
array
(
'month' => 'April-2014',
'total_booking' => 2
),
array
(
'month' => 'May-2014',
'total_booking' => 5
),
array
(
'month' => 'June-2014',
'total_booking' => 25
),
array
(
'month' => 'October-2013',
'total_booking' => 1
),
array
(
'month' => 'July-2014',
'total_booking' => 4
)
);
$start_date="Jan 1 2013";
$end_date="Dec 31 2014";
$timestamp1=strtotime($start_date);
$timestamp2=strtotime($end_date);
for($i=$timestamp1;$i<$timestamp2;$i=$i+24*60*60){
//echo $i;
$gapmonth[]=date('F-Y',$i);
}
$gapmonth=array_unique($gapmonth);
//convert $dataS(ORIGINAL ARRAY) to one dimentional array so the life will be easier
foreach($your_array as $val){
$derived_val[$val['month']]=$val['total_booking'];
}
foreach($gapmonth as $val){
if(array_key_exists($val,$derived_val)){
$total_booking=$derived_val[$val];
}
else{
$total_booking=0;
}
$finaldate[]=array('month'=>$val,'total_booking'=>$total_booking);
}
echo "<pre>";
print_r($finaldate);
OUTPUTS
Array
(
[0] => Array
(
[month] => January-2013
[total_booking] => 0
)
[1] => Array
(
[month] => February-2013
[total_booking] => 0
)
[2] => Array
(
[month] => March-2013
[total_booking] => 0
)
[3] => Array
(
[month] => April-2013
[total_booking] => 0
)
[4] => Array
(
[month] => May-2013
[total_booking] => 0
)
[5] => Array
(
[month] => June-2013
[total_booking] => 0
)
[6] => Array
(
[month] => July-2013
[total_booking] => 0
)
[7] => Array
(
[month] => August-2013
[total_booking] => 0
)
[8] => Array
(
[month] => September-2013
[total_booking] => 0
)
[9] => Array
(
[month] => October-2013
[total_booking] => 1
)
[10] => Array
(
[month] => November-2013
[total_booking] => 0
)
[11] => Array
(
[month] => December-2013
[total_booking] => 0
)
[12] => Array
(
[month] => January-2014
[total_booking] => 0
)
[13] => Array
(
[month] => February-2014
[total_booking] => 0
)
[14] => Array
(
[month] => March-2014
[total_booking] => 0
)
[15] => Array
(
[month] => April-2014
[total_booking] => 2
)
[16] => Array
(
[month] => May-2014
[total_booking] => 5
)
[17] => Array
(
[month] => June-2014
[total_booking] => 25
)
[18] => Array
(
[month] => July-2014
[total_booking] => 4
)
[19] => Array
(
[month] => August-2014
[total_booking] => 0
)
[20] => Array
(
[month] => September-2014
[total_booking] => 0
)
[21] => Array
(
[month] => October-2014
[total_booking] => 0
)
[22] => Array
(
[month] => November-2014
[total_booking] => 0
)
[23] => Array
(
[month] => December-2014
[total_booking] => 0
)
)

Loop through it and test if values are missing:
$prev_month = false;
foreach($your_array as $k=>$values){
if($prev_month!==false){ // first itteration will be empty, dont do useless checks
// Test if themonth after the previous month matches this one
if( $prev_month+1 !== $values['month'] ){
// FILL IN THE BLANKS
// splice in at position $thisIndex (<- this you'll have to do)
array_splice( $prev_month, $thisIndex-1, 0, $newDateBookingsArray); // -1, we want it before this item!
// (dont forget to loop in case more than 1 items miss)
}
}
$prev_month = $values['month']; // save value for next round
}
Edit: TS first asked to sort the array, the code to use a custom sort can be found in the edits of this post.

Related

Group array within group data php

After generating group array from an associative array, now the array is in the following format. And to make this group array I have used the following code:
foreach($upcoming_data_arr as $key => $item)
{
$arr[$item['year']][$key] = $item;
}
Array
(
[2018] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
[2] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[3] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
)
Now I need another group array according to month within year group. And in this case group data array will be like following:
Array
(
[2018] => Array
(
[11] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[2] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
[12] => Array
(
[0] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
)
)
)
How we will get this output?
Add one more "dimension" when you are creating an array identified by $item['month'] and let php decide last "dimension" key by []:
foreach($upcoming_data_arr as $key => $item) {
$arr[$item['year']][$item['month']][] = $item;
}

php find two array values in result multidimensional array

I am looking for last three month state wise data.I have all state code array and last three month array with the following values,
$stateArray = array("Nj","va","Ca","BS","TS");
$MonthArray =array("Nov 2016","Dec 2016","Jan 2017");
Below is my result array fetch from the database,
Array (
[0] => Array (
[month] => Nov 2016
[count] => 150
[state] => NJ
)
[1] => Array (
[month] => Nov 2016
[count] => 100
[state] => va
)
)
I want result like below,
Array(
[Nj] => Array(
[0] => Array(
[month] => Nov 2016
[count] => 150
)
[1] => Array(
[month] => Dec 2016
[count] => 0
)
[2] => Array(
[month] => jan 2017
[count] => 0
)
)
[Ca] => Array(
[0] => Array(
[month] => Nov 2016
[count] => 0
)
[1] => Array(
[month] => Dec 2016
[count] => 0
)
[2] => Array(
[month] => jan 2017
[count] => 0
)
)
[va] => Array(
[0] => Array(
[month] => Nov 2016
[count] => 100
)
[1] => Array(
[month] => Dec 2016
[count] => 0
)
[2] => Array(
[month] => jan 2017
[count] => 0
)
)
)
and so on for all states.
I am trying to array array_search() and in_array() functions for each but it is not working.What I would like to accomplish to loop each state wise array.Below I am trying for looping,
foreach ( $result_array as $val ) {
$month = array_search ( $val ['month'], $monthArray );
$state = array_search ( $val ['state'], $stateArray );
if ($val ['count'] == '' || $val ['count'] == 'NULL') {
$countValue = 0;
} else {
$countValue = $val ['count'];
}
$final_array [] = $countValue;
}
Take a look at this simple example:
<?php
$input = [
[
'month' => "Nov 2016",
'count' => "150",
'state' => "NJ"
],
[
'month' => "Nov 2016",
'count' => "100",
'state' => "va"
],
[
'month' => "Dec 2016",
'count' => "270",
'state' => "NJ"
],
];
$output = [];
foreach (["Nj", "va", "Ca", "BS", "TS"] as $state) {
$output[strtoupper($state)] = [];
};
array_walk($input, function($entry) use (&$output) {
$output[strtoupper($entry['state'])][] = [
'month' => $entry['month'],
'count' => $entry['count']
];
});
print_r($output);
The result of the above code is:
Array
(
[NJ] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 150
)
[1] => Array
(
[month] => Dec 2016
[count] => 270
)
)
[VA] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 100
)
)
[CA] => Array
(
)
[BS] => Array
(
)
[TS] => Array
(
)
)
This appears to be the exact output structure you ask for.
$stateArray = array("Nj","va","Ca","BS","TS");
$MonthArray =array("Nov 2016","Dec 2016","Jan 2017");
$fromDB = Array (
Array (
'month' => 'Nov 2016',
'count' => 150,
'state' => 'NJ'
),
Array (
'month' => 'Nov 2016',
'count' => 100,
'state' => 'va'
)
);
$info = array();
foreach ($fromDB as $row){
$info[strtoupper($row['state'])][$row['month']] = $row['count'];
}
$result = array();
foreach ($stateArray as $state){
foreach ($MonthArray as $month){
$result[$state][]=array('month'=>$month, 'count'=>isset($info[strtoupper($state)][$month])?$info[strtoupper($state)][$month]:0);
}
}
print_r($result);
will output
Array
(
[Nj] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 150
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[va] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 100
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[Ca] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 0
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[BS] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 0
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[TS] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 0
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
)

unset all id keys from multidimensional array

How to unset all keys named id from a multidimensional array?
private function remove_id(Array &$arr){
foreach($arr as $key => $value){
if($key == 'id'){
unset($arr[$key]);
}
elseif(is_array($value)){
$this->remove_id($value);
}
}
}
array
Array
(
[id] => 52453
[periods] => Array
(
[0] => Array
(
[id] => 95296
[is_readonly] => 0
[year] => 2016
[month] => 1
)
[1] => Array
(
[id] => 95297
[is_readonly] => 0
[year] => 2016
[month] => 2
)
[2] => Array
(
[id] => 95298
[is_readonly] => 0
[year] => 2016
[month] => 3
)
[3] => Array
(
[id] => 95299
[is_readonly] => 0
[year] => 2016
[month] => 4
)
[4] => Array
(
[id] => 95300
[is_readonly] => 0
[year] => 2016
[month] => 5
)
[5] => Array
(
[id] => 95301
[is_readonly] => 0
[year] => 2016
[month] => 6
)
[6] => Array
(
[id] => 95302
[is_readonly] => 0
[year] => 2016
[month] => 7
)
[7] => Array
(
[id] => 95303
[is_readonly] => 0
[year] => 2016
[month] => 8
)
[8] => Array
(
[id] => 95304
[is_readonly] => 0
[year] => 2016
[month] => 9
)
[9] => Array
(
[id] => 95305
[is_readonly] => 0
[year] => 2016
[month] => 10
)
[10] => Array
(
[id] => 95306
[is_readonly] => 0
[year] => 2016
[month] => 11
)
[11] => Array
(
[id] => 95307
[is_readonly] => 0
[year] => 2016
[month] => 12
)
)
[closing_profit_amount] => 0
[closing_profit_net_amount] => 0
)
You need to use &value in the for loop to keep changes in nested arrays.
Also the array cannot have more than 1 id key, so there is no need to check it within the loop, which allow a bit of microoptimization here:
private function remove_id(Array &$arr){
if(isset($arr['id'])) {
unset($arr['id']);
}
foreach($arr as &$value){
if(is_array($value)){
$this->remove_id($value);
}
}
}

array php loop. Difficult array. Make a loop

really need your help with this array :
Array
(
[status] => 200
[error] =>
[resource] => Array
(
[type] => stats
[data] => Array
(
[0] => Array
(
[date] => Array
(
[year] => 2015
[month] => 12
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 2.53
[average] => 1.265
)
)
[1] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 15
[average] => 7.5
)
)
[2] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => AUD
[stats] => Array
(
[count] => 1
[total] => 15
[average] => 15
)
)
[3] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => AUD
[stats] => Array
(
[count] => 7
[total] => 1419.02
[average] => 202.71714285714
)
)
[4] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => USD
[stats] => Array
(
[count] => 8
[total] => 2186.4
[average] => 273.3
)
)
[5] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => USD
[stats] => Array
(
[count] => 3
[total] => 865
[average] => 288.33333333333
)
)
[6] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => AUD
[stats] => Array
(
[count] => 19
[total] => 127279
[average] => 6698.8947368421
)
)
)
)
)
How i can get $value of [month] in the loop??
Help me please!
Do you mean you're trying to access the "month" index within date?
If so, you could try something like this:
Let's say $array is your array.
foreach($array['resource']['data'] as $data) {
echo $data['date']['month']; // Prints every month.
echo "<br/>";
}
Hope this helps.
Try array_column function (available since PHP 5.5)
// $arr is the initial array
$months = array_column($arr, 'month');
http://php.net/manual/ru/function.array-column.php

MSSQL / PHP - assistance with query and resultset to get data for bar chart

I am trying to create a bar chart that shows monthly hits for the past 12 months.
I have the below query:
SELECT
(MONTH(date)) AS MONTH
, (YEAR(date)) AS YEAR
, SUM(hits) AS count
FROM [statistics]
WHERE ID = '". $id ."'
AND (date > DATEADD(yy, - 1, GETDATE()))
GROUP BY (MONTH(date)), (YEAR(date))
ORDER BY year, month
This provides the below resultset:
Array
(
[0] => Array
(
[Admin] => Array
(
[month] => 7
[year] => 2012
[count] => 702
)
)
[1] => Array
(
[Admin] => Array
(
[month] => 8
[year] => 2012
[count] => 650
)
)
[2] => Array
(
[Admin] => Array
(
[month] => 9
[year] => 2012
[count] => 670
)
)
[3] => Array
(
[Admin] => Array
(
[month] => 10
[year] => 2012
[count] => 66
)
)
[4] => Array
(
[Admin] => Array
(
[month] => 11
[year] => 2012
[count] => 53
)
)
[5] => Array
(
[Admin] => Array
(
[month] => 12
[year] => 2012
[count] => 39
)
)
[6] => Array
(
[Admin] => Array
(
[month] => 1
[year] => 2013
[count] => 54
)
)
[7] => Array
(
[Admin] => Array
(
[month] => 2
[year] => 2013
[count] => 36
)
)
[8] => Array
(
[Admin] => Array
(
[month] => 3
[year] => 2013
[count] => 48
)
)
)
This only shows the months that it has results for but I need all months whether they have results or not (so count would be 0). What I need is something more like the below which includes all 12 months:
Array
(
[0] => Array
(
[Admin] => Array
(
[month] => 7
[year] => 2012
[count] => 702
)
)
[1] => Array
(
[Admin] => Array
(
[month] => 8
[year] => 2012
[count] => 650
)
)
[2] => Array
(
[Admin] => Array
(
[month] => 9
[year] => 2012
[count] => 670
)
)
[3] => Array
(
[Admin] => Array
(
[month] => 10
[year] => 2012
[count] => 66
)
)
[4] => Array
(
[Admin] => Array
(
[month] => 11
[year] => 2012
[count] => 53
)
)
[5] => Array
(
[Admin] => Array
(
[month] => 12
[year] => 2012
[count] => 39
)
)
[6] => Array
(
[Admin] => Array
(
[month] => 1
[year] => 2013
[count] => 54
)
)
[7] => Array
(
[Admin] => Array
(
[month] => 2
[year] => 2013
[count] => 36
)
)
[8] => Array
(
[Admin] => Array
(
[month] => 3
[year] => 2013
[count] => 48
)
)
[9] => Array
(
[Admin] => Array
(
[month] => 4
[year] => 2013
[count] => 0
)
)
[10] => Array
(
[Admin] => Array
(
[month] => 5
[year] => 2013
[count] => 0
)
)
[11] => Array
(
[Admin] => Array
(
[month] => 6
[year] => 2013
[count] => 0
)
)
)
Can anyone suggest which would be the best way to achieve this? Could it be done in the SQL or does it need to be done in the code? If so, how?
Many thanks in advance.
This can be achieved through PHP using a loop which iterates all the possible months, and if there is data for that month, add it to an output array, otherwise, insert a blank entry with count of 0 for that month
EDIT
Example solution;
$m = $resultset[0]['Admin']['month'];
$y = $resultset[0]['Admin']['year'];
for ($i = 0; $i < 12; $i++) {
$adj = ((($i - 1) + $m) % 12) + 1;
if (!(isset($resultset[$i]['Admin']['month']) && $resultset[$i]['Admin']['month'] == $adj)) {
array_splice($resultset, $i, 0, array(array(
'Admin' => array(
'month' => $adj,
'year' => $y + ($i + $m > 12),
'count' => 0
)
)));
}
}
The important part for solving your issue is the line;
$adj = ((($i - 1) + $m) % 12) + 1;
This translates $i from being an integer between 0-11 to a month between 1-12, depending on the starting month ($m)
Well try this code it may help
foreach ($array as $year => $values) {
for ($i = 1; $i <= 12; $i++) { // Loop through months 1 .. 12
if (!$values[$i]) {
echo "0";
} else {
echo $values[$i];
}
}
}

Categories