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;
}
$val ['month']. Remove the blank character between variable name and brackets.