PHP:在一些算术运算之后合并两个数组

时间:2014-01-14 20:17:37

标签: php arrays multidimensional-array

我需要你的帮助,因为我不知道如何退出这条隧道。

我有这种数组(它们是地方):

$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
        )
 )

请...非常非常感谢你!

1 个答案:

答案 0 :(得分:0)

刚刚解决了。

     $quanti_commenti = count($comments);
        if ($quanti_commenti>0)
        {
            foreach ($comments as $key=>$value)
            {
                $result[$i]['quanti_commenti'] = $quanti_commenti;
                $a = $value['a']*2;
                //
                $somma_parziale = ($a+$b+$c+$d)/11;
                $somma = $somma+$somma_parziale;
                $total = round(($somma/$quanti_commenti),1);
                $result[$i]['total'] = $total;
            }
            $somma = 0;
            $total = 0;
        }
        $i++;