0
我需要請你的幫助,因爲我不知道如何從這個隧道退出。PHP:經過一些算術操作合併兩個陣列
我有這個有點數組(它們是地方):
$places =
Array
(
[0] => Array
(
[id] => 3
[route] => Via dei Gelsi
[street_number] => 1
[postal_code] => 00171
[administrative_area_level_3] => Roma
[administrative_area_level_2_short] => RM
)
[1] => Array
(
[id] => 6
[route] => Via Ugo Bartolomei
[street_number] => 6
[postal_code] => 00136
[administrative_area_level_3] => Roma
[administrative_area_level_2_short] => RM
)
[2] => Array
(
[id] => 8
[route] => Via di San Giovanni in Laterano
[street_number] => 20
[postal_code] => 00184
[administrative_area_level_3] => Roma
[administrative_area_level_2_short] => RM
)
)
而且每個地方都有0,1或更多的評論:
$comments =
Array
(
[0] => Array
(
[0] => Array
(
[id] => 4
[id_place] => 3
[id_user] => 1
[a] => 1
[b] => 3
[c] => 2
[d] => 4
)
[1] => Array
(
[id] => 11
[id_place] => 3
[id_user] => 1
[a] => 5
[b] => 5
[c] => 4
[d] => 3
)
)
[1] => Array
(
[0] => Array
(
[id] => 5
[id_place] => 6
[id_user] => 1
[a] => 5
[b] => 4
[c] => 5
[d] => 4
)
[1] => Array
(
[id] => 6
[id_place] => 6
[id_user] => 1
[a] => 1
[b] => 1
[c] => 1
[d] => 1
)
[2] => Array
(
[id] => 7
[id_place] => 6
[id_user] => 1
[a] => 1
[b] => 4
[c] => 3
[d] => 2
)
)
[2] => Array
(
)
[3] => Array
(
[0] => Array
(
[id] => 13
[id_place] => 9
[id_user] => 1
[a] => 1
[b] => 1
[c] => 1
[d] => 1
)
)
[4] => Array
(
)
)
我需要執行一些還挺操作,以僞碼爲例:
foreach ($comments as $key=>$value)
{
$how_many_comments = count how many comments have same $value['id_place']
// for $comments[0] the result is 2
// pick every a, b, c, d, multiplicate for 2, 2, 3 and 4 and sign result
$a = $value['a']*2;
$b = $value['b']*2;
$c = $value['c']*3;
$d = $value['d']*4;
// and so on
$sum_partial = ($a+$b+$c+$d)/11;
$sum = $sum+$partial_sum;
$total = round($sum/$how_many_comments),1)
// set in first array ($place) $total (calculated as 3.3) where $comments['id_place'] == $result['id']
}
因此,最終數組$ place需要類似於:
Array
(
[0] => Array
(
[id] => 3
[route] => Via dei Gelsi
[street_number] => 1
[postal_code] => 00171
[administrative_area_level_3] => Roma
[administrative_area_level_2_short] => RM
[total] = 3.3
[num_comments] = 2
)
)
請......非常非常感謝!