2

I have an array of people that looks like this:

$people =
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
                [count] => 0
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
                [count] => 0
            )
    )

And I have an array which is the result of a MySQL query that looks like this:

$query=
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
            )

        [2] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
            )

    )

For each e-mail address in $people, I'd like $people['count'] to equal how many times that e-mail address occurs in $query.

I've tried loads of ways to do this, and I'm not quite getting the desired result.

For the avoidance of doubt, my end result based on the example above should look like:

$people =
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
                [count] => 2
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
                [count] => 1
            )
    )
4

2 に答える 2

4
foreach ($people as $key => $man) { // iterating through each man
  $_occurences = 0;

  foreach ($query as $_item) // iterating through each result in the result set
    if ($_item['email'] == $man['email']) // comparing current man to each result item
      $_occurences ++;

  $people[$key]['count'] = $_occurences; // saving number of occurrences in the `count` key 

 }

UPD:と nice演算子を使用array_mapしたもう 1 つのソリューション。よりも遅くなりますが、2 倍コンパクトでプロフェッショナルです。array_reduceternaryforeach

(パフォーマンスの点で)遅いです。関数呼び出しのオーバーヘッドが減少しますが、「少量」の反復では、この低下は無視できます。$people配列を再割り当てせずに更新します。それぞれを参照して送信&$manしました。

array_map(function (&$man) use ($query) {
    $man['count'] = array_reduce($query, function ($count, $row) use ($man) {
        return ($row['email'] === $man['email']) ? ++$count : $count ;
    }, 0);
}, $people);
于 2012-08-12T11:39:55.580 に答える
1

それがまさに必要なものかどうかはわかりませんが、実行しているクエリを次のように変更できます。

SELECT COUNT(name) as cnt, name,email FROM table_name GROUP BY name;

配列は次$queryのようになります。

$query=
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
                [cnt] => 2
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
                [cnt] => 1
            )                  
    )

次に、配列で実行して、配列cnt内の各項目を取得するだけ$queryです。

于 2012-08-12T11:51:42.107 に答える