dongzhiman2162 2014-05-30 19:56
浏览 156
已采纳

PHP计数字符串作为数组内部的值出现的次数

So I looked around here for a bit but cant seem to get this right, so I am forced to make a new post. I gave it an effort! Don't hurt me.

Sample Array:

$ndata = Array
(
    [ARMOR_HEAD] => Andariel's Visage
    [ARMOR_TORSO] => Blackthorne's Surcoat
    [ARMOR_FEET] => Illusory Boots
    [ARMOR_HANDS] => Gauntlets of Akkhan
    [ARMOR_SHOULDERS] => Pauldrons of Akkhan
    [ARMOR_LEGS] => Blackthorne's Jousting Mail
    [ARMOR_BRACERS] => Nemesis Bracers
    [ARMOR_MAINHAND] => Gyrfalcon's Foote
    [ARMOR_OFFHAND] => Jekangbord
    [ARMOR_WAIST] => Blackthorne's Notched Belt
    [ARMOR_RRING] => Ring of Royal Grandeur
    [ARMOR_LRING] => Stone of Jordan
    [ARMOR_NECK] => Kymbo's Gold
)

  $count = count(preg_grep('Akkhan', $ndata));
  print_r($count);

So this is only returns 1 instead of 2. I also tried array_search(), but that simply returns a the first found with its key. Or in_array but that is just boolean I guess.. Is there a better way to go about this?

  • 写回答

1条回答 默认 最新

  • duan5731 2014-05-30 20:04
    关注

    The first parameter to preg_grep is not a string, it is a regular expression pattern represented as a string. Try this:

    preg_grep('/Akkhan/i', $ndata)
    

    For more information, see the documentation page for preg_grep.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)