计算数组中数组中元素的数量

时间:2015-04-23 11:20:00

标签: php arrays laravel

我有问题。我正在尝试使用主数组

中的数组中的特定元素来计算数组中的数组键
public function index()
{   
            $user=User::all();
            $lead=Lead::all();
            $role=Role::all();
            $lead=DB::table('lead')
                 ->select(DB::raw('lead.id','users.number','users.username', 'lead.leadid','lead.first_name', 'lead.last_name','lead.email' ,count('lead.email') ))
                 ->where ('lead.email', '>', 1)
                 ->groupBy('lead.email')
                 ->leftJoin('users', 'users.number', '=', 'lead.assign')
                 ->get();
                $user=DB::table('users')->select('users.number','users.username')->get();

                echo "<pre>";
                print_r($lead);
                die();
}

这是打印出来时的输出

Array
(
[0] => Array
    (
        [id] => 63
        [number] => 3
        [username] => shankar
        [leadid] => zcrm_125720000016007771
        [first_name] => 
        [last_name] => Amoah Owusu Richmond
        [email] => ramoahhowusu50@gmail.com
    )

[1] => Array
    (
        [id] => 64
        [number] => 3
        [username] => shankar
        [leadid] => zcrm_125720000016007733
        [first_name] => Deus
        [last_name] => mathew
        [email] => mathewdeus@gmail.com
    )

[2] => Array
    (
        [id] => 65
        [number] => 2
        [username] => james
        [leadid] => zcrm_125720000016007737
        [first_name] => bari
        [last_name] => safi
        [email] => barisafi57@gmail.com
    )

[3] => Array
    (
        [id] => 66
        [number] => 11
        [username] => nishupandey
        [leadid] => zcrm_125720000016007741
        [first_name] => Noorahmad
        [last_name] => Noor
        [email] => noorahmad.noor81@gmail.com
    )

[4] => Array
    (
        [id] => 67
        [number] => 12
        [username] => ravi123
        [leadid] => zcrm_125720000016007747
        [first_name] => munsanje
        [last_name] => nakeempa
        [email] => mnakeempa@yahoo.com
    )

[5] => Array
    (
        [id] => 68
        [number] => 8
        [username] => veerkishor
        [leadid] => zcrm_125720000016007751
        [first_name] => Noorahmad
        [last_name] => Noor
        [email] => noorahmad.noor71@gmail.com
    )

[6] => Array
    (
        [id] => 69
        [number] => 13
        [username] => rahul
        [leadid] => zcrm_125720000016007755
        [first_name] => painad
        [last_name] => sherzad
        [email] => painda12sherzad@gmail.com
    )
)

我想使用电子邮件计算元素,并检查元素在数组中出现的次数。我试图使用array_count_values但不太确定如何使用它。提前致谢

4 个答案:

答案 0 :(得分:1)

试试这个:

在你的DB :: raw中,你需要在引号内加上“count”。否则,您将只运行php count(如sizeof)。

<?php

    $lead=DB::table('lead')
    ->select(['lead.id','users.number','users.username', 'lead.leadid','lead.first_name', 'lead.last_name','lead.email'])
     ->select( DB::raw( "count('lead.email') as total") )
     ->where ('lead.email', '>', 1)
     ->groupBy('lead.email')
     ->leftJoin('users', 'users.number', '=', 'lead.assign')
     ->get();

答案 1 :(得分:0)

要计算有多少元素具有相同的电子邮件,我猜您必须手动执行...

$emails = array();
foreach ($lead as $lineNumber => $line) {
    $email = $line['email'];
    if(array_key_exists("$email",$emails) {
        $emails["$email"]++;
    } else {
        $emails["$email"] = 1;
    }
}
echo "<pre>";
print_r($emails);

这就是你想要的吗?

答案 2 :(得分:0)

如果要计算大数组中具有特定属性的数组的数量,则需要遍历所有外部数组元素并检查内部数组元素。

答案 3 :(得分:0)

仅使用您要计数的Count($ array) 希望它能工作