我想要一种简单的方法来计算字符串“Apple”在给定中出现的次数
# My Array :
$arr = array(
1 => "Apple",
2 => "Orange",
3 => array(1=>"Bananna",2=>"Apple"),
4 => "Grape",
5 => "Apple",
6 => array(1=>"Grape"),
7 => "Orange");
# Want to count only "Apple"
$needle = "Apple";
# My Function :
function arrsearch($needle,$haystack,$return) {
if(!is_array($haystack)) {
return false;
}
foreach($haystack as $key=>$val) {
if(is_array($val)) {
$return = arrsearch($needle,$val,$return);
}else if(strtolower($val) == strtolower($needle)) {
$return[] = $key;
}
}
return $return;
}
$var = arrsearch("Apple",$arr,array());
echo " Output : ".count($var);
# Output : 3
我使用上面的函数来查找数组中字符串“Apple”的次数。给我推荐一个最好的。