dongqiancui9194 2016-06-05 14:40
浏览 98
已采纳

PHP - strpos对我不起作用?

So I am trying to search through JSON, using strpos for it, but it doesn't for me. I always get that "Items doesn't exist".

Here's the PHP

$getItems = file_get_contents('Items.json');
$decodeItems = json_decode($getItems,true);

//$output = ''.$decodeItems['items'][0]['name'].'';
$output = '';

if(isset($_POST['search'])){
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

    foreach($decodeItems['items'] as $data){
        if($chance = strpos($data, $searchq) !== FALSE){
            if($data['name'] == $chance){
                $name = $data['name'];
                $output .= "<div>".$name."</div>";
            }
        }
        else{
            $output = 'Items no';
        }
    }
}

Here's the Example JSON

{"img":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQh5hlcX0nvUOGsx8DdQBJjIAVHubSaIAlp1fb3YihQ-tWglYy0lfjjOr6fxjpQ7MFz373Fodyl0AXh-ENkMWinJ4eXcA8-ZFHUq1K_xum70ZO56oOJlyUgjHI5fA","name":"&#x2605 Bowie Knife","assetid":"6442574944","myprice":"155.36","condition":"","originalname":"\u2605 Bowie Knife","inspect":"steam:\/\/rungame\/730\/76561202255233023\/+csgo_econ_action_preview%20S76561198034643020A6442574944D2305887113904442996","special":"0","floatvalue":null,"bitskinsprice":"117.36"}
  • 写回答

1条回答 默认 最新

  • donglu9134 2016-06-05 14:46
    关注

    From the manual the return value of strpos:

    Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

    Returns FALSE if the needle was not found.

    So, I suggest you to change this part of your code:

    if(strpos($data['name'], $searchq) === true)
    

    to

    if(strpos($data['name'], $searchq) !== FALSE)
    

    Updating my answer according to the comments below.

    The JSON needs to follow the following notation in order for the code above to work:

    {"items":[
       {
          "img":"img1",
          "name":"name1",
          "assetid":"1",
          "myprice":"155.36",
          "condition":"",
          "originalname":"name1 original",
          "inspect":"whatever",
          "special":"0",
          "floatvalue":null,
          "bitskinsprice":"117.36"
       },
       {
          "img":"img2",
          "name":"name2",
          "assetid":"2",
          "myprice":"175.11",
          "condition":"",
          "originalname":"name2 original",
          "inspect":"whatever2",
          "special":"0",
          "floatvalue":null,
          "bitskinsprice":"55.55"
       }
    ]};
    

    Updating my answer according to the comments below.

    OK, I made some changes to your script to deal correctly with the JSON file.

    $getItems = file_get_contents('Items.json');
    $decodeItems = json_decode($getItems,true);
    
    //$output = ''.$decodeItems['items'][0]['name'].'';
    $output = '';
    
    if(isset($_POST['search'])){
        $searchq = $_POST['search'];
        $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    
        foreach($decodeItems['items'] as $data){
           // this was *if($chance = strpos($data, $searchq) !== FALSE){*
           if(strpos($data['name'], $searchq) !== FALSE) {
              // here was another unneeded *if($data['name'] == $chance){*
              $name = $data['name'];
              $output .= "<div>".$name."</div>";
           }
        }
    
        if (empty($output)) {
          $output = 'Items no';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景