我有2个这样的PHP数组:
array (size=12)
0 => string 'April 2017' (length=10)
1 => string 'Mei 2017' (length=8)
2 => string 'Juni 2017' (length=9)
3 => string 'Juli 2017' (length=9)
4 => string 'Augustus 2017' (length=13)
5 => string 'September 2017' (length=14)
6 => string 'Oktober 2017' (length=12)
7 => string 'November 2017' (length=13)
8 => string 'December 2017' (length=13)
9 => string 'Januari 2018' (length=12)
10 => string 'Februari 2018' (length=13)
11 => string 'Maart 2018' (length=10)
array (size=8)
0 =>
object(stdClass)[81]
public 'label' => string 'April 2017' (length=10)
public 'vacyear' => string '2017' (length=4)
public 'vacmonth' => string '4' (length=1)
public 'totalsubscriptions' => string '2' (length=1)
1 =>
object(stdClass)[82]
public 'label' => string 'Oktober 2017' (length=12)
public 'vacyear' => string '2017' (length=4)
public 'vacmonth' => string '10' (length=2)
public 'totalsubscriptions' => string '5' (length=1)
2 =>
object(stdClass)[83]
public 'label' => string 'November 2017' (length=13)
public 'vacyear' => string '2017' (length=4)
public 'vacmonth' => string '11' (length=2)
public 'totalsubscriptions' => string '1' (length=1)
3 =>
object(stdClass)[84]
public 'label' => string 'December 2017' (length=13)
public 'vacyear' => string '2017' (length=4)
public 'vacmonth' => string '12' (length=2)
public 'totalsubscriptions' => string '13' (length=2)
4 =>
object(stdClass)[85]
public 'label' => string 'Januari 2018' (length=12)
public 'vacyear' => string '2018' (length=4)
public 'vacmonth' => string '1' (length=1)
public 'totalsubscriptions' => string '14' (length=2)
5 =>
object(stdClass)[86]
public 'label' => string 'Februari 2018' (length=13)
public 'vacyear' => string '2018' (length=4)
public 'vacmonth' => string '2' (length=1)
public 'totalsubscriptions' => string '31' (length=2)
6 =>
object(stdClass)[87]
public 'label' => string 'Maart 2018' (length=10)
public 'vacyear' => string '2018' (length=4)
public 'vacmonth' => string '3' (length=1)
public 'totalsubscriptions' => string '4' (length=1)
7 =>
object(stdClass)[88]
public 'label' => string 'April 2018' (length=10)
public 'vacyear' => string '2018' (length=4)
public 'vacmonth' => string '4' (length=1)
public 'totalsubscriptions' => string '5' (length=1)
第一个数组是$past12months
,它总是包含12个元素(过去12个月中每个月)。
从mysql中检索第二个数组$orgvacssubscrmonth
并包含带有标签的对象数组(这与$past12months
数组中的确切条目匹配。
我需要合并这些数组以创建如下所示的结果:
array (size=2)
'months' =>
array (size=12)
0 => string 'April 2017' (length=10)
1 => string 'Mei 2017' (length=8)
2 => string 'Juni 2017' (length=9)
3 => string 'Juli 2017' (length=9)
4 => string 'Augustus 2017' (length=13)
5 => string 'September 2017' (length=14)
6 => string 'Oktober 2017' (length=12)
7 => string 'November 2017' (length=13)
8 => string 'December 2017' (length=13)
9 => string 'Januari 2018' (length=12)
10 => string 'Februari 2018' (length=13)
11 => string 'Maart 2018' (length=10)
'amounts' =>
array (size=12)
0 => string '2' (length=1)
1 => int 0
2 => int 0
3 => int 0
4 => int 0
5 => int 0
6 => string '5' (length=1)
7 => string '1' (length=1)
8 => string '13' (length=2)
9 => string '14' (length=2)
10 => string '31' (length=2)
11 => string '4' (length=1)
这是我到目前为止的代码:
foreach ($past12months as $month) {
if(!in_array($month, $past12monthsfulldata['months'], true)){
array_push($past12monthsfulldata['months'], $month);
foreach ($orgvacssubscrmonth as $entry) {
if ($entry->label == $month) {
array_push($past12monthsfulldata['amounts'], $entry->totalsubscriptions);
break;
} else {
array_push($past12monthsfulldata['amounts'], 0);
break;
}
}
}
}
这会产生如下结果(因为我想假设中断,但是如果我删除它们,我会将我的数量数组用12x6元素组成):
array (size=2)
'months' =>
array (size=12)
0 => string 'April 2017' (length=10)
1 => string 'Mei 2017' (length=8)
2 => string 'Juni 2017' (length=9)
3 => string 'Juli 2017' (length=9)
4 => string 'Augustus 2017' (length=13)
5 => string 'September 2017' (length=14)
6 => string 'Oktober 2017' (length=12)
7 => string 'November 2017' (length=13)
8 => string 'December 2017' (length=13)
9 => string 'Januari 2018' (length=12)
10 => string 'Februari 2018' (length=13)
11 => string 'Maart 2018' (length=10)
'amounts' =>
array (size=12)
0 => string '2' (length=1)
1 => int 0
2 => int 0
3 => int 0
4 => int 0
5 => int 0
6 => int 0
7 => int 0
8 => int 0
9 => int 0
10 => int 0
11 => int 0
答案 0 :(得分:1)
您可以删除else子句,以便循环中的未来迭代可以执行所需的插入:
foreach ($past12months as $month) {
if(!in_array($month, $past12monthsfulldata['months'], true)){
array_push($past12monthsfulldata['months'], $month);
foreach ($orgvacssubscrmonth as $entry) {
if ($entry->label == $month) {
array_push($past12monthsfulldata['amounts'], $entry->totalsubscriptions);
break;
}
}
}
}