从二维数组中计算某个字符串的出现频率

<?php

$array=array(
             array("name","word","hello","haha"),
             array("user","push","array","pop"),
             array("code","course","content","public"),
    );

#计算该数组中 o 出现的频率

$num=0;
// 1.常规解法 遍历 时间复杂度 m*n
// foreach ($array as $k => $v)
// {
//     foreach ($v as $val)
//     {
//         if(strpos($val,"o")!==false)
//         {
//             $num++;
//         }
//     }
// }

// 2.运用函数 减少循环 时间复杂度m
// foreach ($array as $k => $v)
// {
//     $string=implode("",$v);//数组变字符串
//     $num+= substr_count($string,'o');//查找出现的次数并累加
// }

// 3.运用json 剔除循环 时间复杂度 1
// $string=json_encode($array);
// $num+= substr_count($string,'o');



echo $num;

?>
原文地址:https://www.cnblogs.com/lizhaoyao/p/5757682.html