duanpuqi9965 2015-04-23 11:19
浏览 307
已采纳

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

I have a problem. I am trying to count the array keys in my array using an specific element inside my array in the main array

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();
}

Here is the output when I print it out

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

I want to count the elements using the email and check how many times the element is appearing in the array. Am trying to use the array_count_values but am not quite sure how to use it. Thanks in advance

  • 写回答

4条回答 默认 最新

  • doufangpian5545 2015-04-23 12:50
    关注

    Try this:

    In your DB::raw, you need to put "count" inside quotes. Otherwise, you will just run php count (like 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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化