[英]How to Cross check one array values into another associative multidimensional Array
[英]Count or sum values from one multidimensional associative array using another
我有兩個具有相同鍵的多維關聯數組。 我需要根據另一個數組的要求對一個數組中的特定值進行計數/求和。
我已嘗試嵌套的foreach循環但無法獲取請求的結果。 我已經在互聯網上搜索了我最好的能力而沒有成功。
這是我的代碼無法按要求工作:
foreach ($b as $key => $value) {
foreach ($a as $skey => $svalue) {
if ($key == $skey) {
foreach ($svalue[Season] as $ssvalue) {
if ($value[Appearance] == $ssvalue) {
$c[$key]['SeasonAppearances'][] = count($value[Appearance]);
}
}
}
}
}
第一個數組(提取)/ $ a
$a = Array (
[Paul] => Array (
[Season] => Array (
[0] => 2014
[1] => 2015
[2] => 2016
)
)
[John] => Array (
[Season] => Array (
[0] => 2012
)
)
)
第二個數組(提取)/ $ b
$b = Array (
[Paul] => Array (
[Appearance] => Array (
[0] => 2014
[1] => 2014
[2] => 2014
[3] => 2014
[4] => 2014
[5] => 2014
[6] => 2014
[7] => 2014
[8] => 2015
[9] => 2015
[10] => 2015
)
)
[John] => Array (
[Appearance] => Array (
[0] => 2012
[1] => 2012
[2] => 2012
[3] => 2012
)
)
)
請求的結果數組/ $ c
$c = Array (
[Paul] => Array (
[SeasonCounts] => Array (
[0] => 8
[1] => 3
[2] => 0
)
)
[John] => Array (
[SeasonCounts] => Array (
[0] => 4
)
)
)
我想計算每個季節$ b匹配每個人的出現次數$ a並創建結果數組。 請注意保羅在2016年沒有露面。
無條件,功能性方法:( 演示 )
$season_data = [
'Paul' => [
'Season' => [2014, 2015, 2016]
],
'John' => [
'Season' => [2012]
]
];
$appearance_data = [
'Paul' => [
'Appearance'=> [
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015
]
],
'John' => [
'Appearance' => [
2012, 2012, 2012, 2012
]
]
];
foreach ($season_data as $person => $data) {
$defaults = array_fill_keys($data['Season'], 0);
$result[$person]['SeasonCounts'] = array_replace(
$defaults,
array_intersect_key(
array_count_values($appearance_data[$person]['Appearance']),
$defaults
)
);
}
var_export($result);
輸出:
array (
'Paul' =>
array (
'SeasonCounts' =>
array (
2014 => 8,
2015 => 3,
2016 => 0,
),
),
'John' =>
array (
'SeasonCounts' =>
array (
2012 => 4,
),
),
)
使用此任務的功能可以明確正在執行的任務 - 使未來的開發人員可以輕松理解代碼。
array_count_values()
來快速生成一個外觀年份數和它們的計數(這是函數生成的確切任務)。 array_intersect_key()
保留人員季節數組中指定的年份。 array_replace()
將任意零替換為新的相應計數值。 作為對上述內容的略微修改,您可以在計算之前進行過濾: Demo 。
每個人只需要四個功能干凈,可讀的功能調用,以獲得准確的預期結果。
我的結果數組是使用關聯的SeasonCounts
子數組設計的,因為數據是字面上的關聯。 使用索引計數存儲輸出會放松關系 - 我的方法可以避免這種損失。
或者,您可以為每個人每次出現一個季節進行迭代過濾和計數調用。 ( 相同的結果 )
foreach ($season_data as $person => $data) {
foreach ($data['Season'] as $year) {
$result[$person]['SeasonCounts'][$year] = sizeof(
array_filter(
$appearance_data[ $person]['Appearance'],
function($v) use ($year) {
return $v == $year;
})
);
}
}
最后是一種無功能的條件遞增方法。 ( 相同的結果 )
foreach ($season_data as $person => $data) {
foreach ($data['Season'] as $year) {
$result[$person]['SeasonCounts'][$year] = 0;
foreach ($appearance_data[$person]['Appearance'] as $appearance) {
if ($appearance == $year) {
++$result[$person]['SeasonCounts'][$year];
}
}
}
}
這應該算得上你想要它
$c = array();
$helper = array();
foreach ($b as $key => $value) {
$c[$key] = array();
$c[$key]["SeasonCount"] = array();
$helper[$key] = array();
foreach ($value["Appearance"] as $key1 => $value1) {
$position = array_search($value1, $helper[$key]);
if (false !== $position) {
$c[$key]["SeasonCount"][$position]++;
} else {
$helper[$key][] = $value1;
$c[$key]["SeasonCount"][] = 1;
}
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.