这是我的第一个问题!
$champion1greenwards = 0;
$champion2greenwards = 0;
$champion3greenwards = 0;
$champion4greenwards = 0;
$champion5greenwards = 0;
$champion6greenwards = 0;
$champion7greenwards = 0;
$champion8greenwards = 0;
$champion9greenwards = 0;
$champion10greenwards = 0;
foreach($json as $arrays) {
if (is_array($arrays)) {
foreach($arrays as $arrayx) {
foreach($arrayx as $arrayt) {
foreach($arrayt as $array) {
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '1') {
$champion1greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '2') {
$champion2greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '3') {
$champion3greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '4') {
$champion4greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '5') {
$champion5greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '6') {
$champion6greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '7') {
$champion7greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '8') {
$champion8greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '9') {
$champion9greenwards++;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD' && $array['creatorId'] == '10') {
$champion10greenwards++;
}
}
}
}
}
}
$championgreenwards = array($champion1greenwards,$champion2greenwards,$champion3greenwards,$champion4greenwards,$champion5greenwards,$champion6greenwards,$champion7greenwards,$champion8greenwards,$champion9greenwards,$champion10greenwards);
结果$ championgreenwards数组:
Array
(
[0] => 0
[1] => 14
[2] => 0
[3] => 0
[4] => 20
[5] => 0
[6] => 16
[7] => 0
[8] => 0
[9] => 23
)
使用这段代码,我可以计算在地图中放置1,2,3,4,5,6,7,8,9,10个病房的次数,然后我用数据制作一个数组。但代码太长了,我不知道如何缩短这段代码。我找不到办法做到这一点。所以我来到这里。
答案 0 :(得分:5)
这样的事情:
$greenwards = [];
foreach($json as $arrays) {
if (is_array($arrays)) {
foreach($arrays as $arrayx) {
foreach($arrayx as $arrayt) {
foreach($arrayt as $array) {
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD') {
$greenwards[$array['creatorId']]++;
}
}
}
}
}
}
答案 1 :(得分:2)
$championgreenwards = array_fill(0, 10, 0);
foreach($json as $arrays) {
if (!is_array($arrays)) {
continue;
}
foreach($arrays as $arrayx) {
foreach($arrayx as $arrayt) {
foreach($arrayt as $array) {
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD') {
$championgreenwards[$array['creatorId']-1]++;
}
}
}
}
}
var_dump($championgreenwards);
或使用array_walk_recursive
,如下所示:
array_walk_recursive($json, function ($array) use (&$championgreenwards) {
if (!is_array($array) || !isset($array['type']) || !isset($array['wardType'])) {
return;
}
if ($array['type'] == 'WARD_PLACED' && $array['wardType'] == 'SIGHT_WARD') {
$championgreenwards[$array['creatorId']-1]++;
}
});