2017-01-04 25 views
0

通知システムを作成しています。以下に示す結果は、特定のユーザーのすべての通知を返します。私は配列を 'notify_id'でグループ化し、グループ内の各項目を数えたいと思います。Phpグループ値とグループの配列グループ

私が持っているだろうというような:


"ジャック・ビルは今、あなたをフォローしています"、ジョン・ドウはあなたのポスト2" 上のコメント、 "ジョン・ドウとコメント2人の他人 1" あなたのポストに

Array ([0] => 
Array ([id] => 1 
     [user_id] => 10 
     [type] => follow 
     [notify_id] => 13 
     [notify_date] => 1483444712 
     [firstname] => Jack 
     [surname] => Bill 
     [picture] =>) 



     [1] => 
Array ([id] => 10 
     [user_id] => 10 
     [type] => comment 
     [notify_id] => 2 
     [notify_date] => 1482159309 
     [firstname] => John 
     [surname] => Doe 
     [picture] =>) 

     [2] => 
Array ([id] => 8 
     [user_id] => 10 
     [type] => comment 
     [notify_id] => 1 
     [notify_date] => 1482159219 
     [firstname] => John 
     [surname] => Doe 
     [picture] =>) 

     [3] => 
Array ([id] => 6 
     [user_id] => 16 
     [type] => comment 
     [notify_id] => 1 
     [notify_date] => 1482159129 
     [firstname] => James 
     [surname] => Canon 
     [picture] =>) 

     [4] => 
Array ([id] => 5 
     [user_id] => 14 
     [type] => comment 
     [notify_id] => 1 
     [notify_date] => 1482159079 
     [firstname] => Sharon 
     [surname] => Abba 
     [picture] =>) ) 
+0

あなたの質問は何ですか? –

+1

あなたはそれを格納している場所から必要なフォーマットでデータを取得してみませんか? – Kisaragi

+1

どのようにこれらの配列を引っ張っていますか?それらを 'notify_id'でグループ化したいのであれば、あなたのクエリでそれを行うべきです – WillardSolutions

答えて

0

次の2つの質問があり、私はそれらの両方に答えることをしようとします。

私はグループに 'notify_id' によって、配列にしたい

...

は、ご使用のアレイの名前が$aであると仮定すると:

$return = array(); 
foreach($a as $val) { 
    if (!isset($return[$val['notify_id']])) { 
     $return[$val['notify_id']] = array(); 
    } 
    $return[$val['notify_id']][] = $val; 
} 
print_r($return); // <-- Your array is grouped by notify_id 

...ともに各項目を数えますグループ。今、あなたはあなたの$returnnotify_idでグループ化されている

ので:

foreach($return as $k => $v) { 
    echo count($v) . ' values are present for notify #' . $k; 
    // It will display something like: 10 values are present for notify #1 
} 

は、それが役立つ幸運を願っています!

+0

Thanks @ rap-2-h。それはうまくいった! –