PHP - count specific array values

前端 未结 10 1556
野的像风
野的像风 2020-12-02 18:00

How can I count the number of element inside an array with value equals a constant? example,

$myArray = array(\"Kyle\",\"Ben\",\"Sue\",\"Phil\",\"Ben\",\"Mar         


        
10条回答
  •  渐次进展
    2020-12-02 18:41

    In my case of two dimensional array, from PHP Official Page comments, I found the useful snippet for a two dimensional array-

     1, 'userId' => 5],
      ['id' => 2, 'userId' => 5],
      ['id' => 3, 'userId' => 6],
    ];
    $userId = 5;
    
    echo array_count_values(array_column($list, 'userId'))[$userId]; // outputs: 2
    ?>
    

提交回复
热议问题