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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘