我正在寻找最近三个月的状态明智数据。我有所有状态代码数组和最后三个月的数组,其中包含以下值:
$stateArray = array("Nj","va","Ca","BS","TS");
$MonthArray =array("Nov 2016","Dec 2016","Jan 2017");
以下是我从数据库中获取的结果数组
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
)
)
)
所有州的等等。
我正在尝试为每个函数添加array_search()
和in_array()
函数,但它不起作用。我想完成循环每个状态明智的数组。我正在尝试循环,
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;
}
答案 0 :(得分:1)
看看这个简单的例子:
<?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);
上述代码的结果是:
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
(
)
)
这似乎是您要求的确切输出结构。
答案 1 :(得分:1)
$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);
将输出
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
)
)
)