doumi0737 2013-07-22 01:20
浏览 17
已采纳

stristr生成的重复条目

I keep getting duplicate entries (4 to be exact) when using stristr to filter a JSON by keywords. An example of my code is as follows:

$keywords = array('small','medium','large');

foreach($keywords as $keyword) {
foreach ($data as &posts) {
    if(stristr($posts['message'],$keyword) !== FALSE ) {
        print_r($posts);
        }
        }
        }

When I do the following there are zero duplicates:

foreach ($data as &posts) {
    print_r($posts);
    }

I have tried array_uniq but with no success. Can someone please point me in the right direction? Is there a better way to sort JSON?

  • 写回答

1条回答 默认 最新

  • doukai2839 2013-07-22 01:44
    关注

    Do the duplicate posts match more than one keyword? If so, they'll be printed for each match.

    If you want to print each post only once per match, loop through the posts first, and quit checking a post when a keyword match is found:

    foreach ($data as &posts) {
      foreach($keywords as $keyword) {
        if(stristr($posts['message'],$keyword) !== FALSE ) {
            print_r($posts);
            break;
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求