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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊