PHP-strpos不';我不能正常工作吗?
因此,我尝试通过JSON进行搜索,使用strpos进行搜索,但对我来说不是这样。我总是觉得“物品不存在” 这里是PHPPHP-strpos不';我不能正常工作吗?,php,json,Php,Json,因此,我尝试通过JSON进行搜索,使用strpos进行搜索,但对我来说不是这样。我总是觉得“物品不存在” 这里是PHP $getItems = file_get_contents('Items.json'); $decodeItems = json_decode($getItems,true); //$output = ''.$decodeItems['items'][0]['name'].''; $output = ''; if(isset($_POST['search'])){
$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';
}
}
}
从手册中,返回以下值: 返回针相对于干草堆字符串开头的位置(与偏移无关)。还要注意,字符串位置从0开始,而不是从1开始 如果未找到指针,则返回FALSE 因此,我建议您更改代码的这一部分:
if(strpos($data['name'], $searchq) === true)
到
根据下面的评论更新我的答案
JSON需要遵循以下符号才能使上述代码正常工作:
{"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"
}
]};
根据下面的评论更新我的答案
好的,我对您的脚本做了一些更改以正确处理JSON文件
$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';
}
}
$getItems=file_get_contents('Items.json');
$decodeItems=json_decode($getItems,true);
//$output='.$decodeItems['items'][0]['name'].';
$output='';
如果(isset($_POST['search'])){
$searchq=$_POST['search'];
$searchq=preg#u replace(“#[^0-9a-z]#i”,”,$searchq);
foreach($decodeItems['items']作为$data){
//这是*if($chance=strpos($data,$searchq)!==FALSE){*
if(strpos($data['name',$searchq)!==FALSE){
//这是另一个不必要的*if($data['name']==$chance){*
$name=$data['name'];
$output.=''.$name.'';
}
}
if(空($输出)){
$output='项目编号';
}
}
strpos()
将永远不会返回true,因此检查将始终失败。我在代码中没有看到任何与JSON相关的内容(可能$data来自JSON,但这是不相关的,显示$data而不是谈论JSON)$数据来自foreach loopHmm,谢谢。如果我更改了它并删除了那个['name']部分,但它的工作方式不正确。如果我在那里键入项目名称,那么它会显示所有项目,但我希望它只显示那些与strpo匹配的项目。因此,如果我在那里键入Bowie,那么它会显示Bowie一词所在的项目。看起来你并没有真正解码你的JSON项目。你应该首先执行$decode items=JSON\u decode($jsonString);这将使用项数组填充$decodeItems,其中每个项都有自己的name属性,然后您的脚本将以上面相同的方式继续。我是,我刚刚发布了“一点”代码。我将发布所有内容。编辑了我的第一篇文章:)有一件事,上面附加的JSON字符串是一个项目的字符串吗?或者这是您使用的同一个JSON?因为该字符串中不包含任何项目,在我看来它是一个项目。当JSON_decode处理该项目时,$decodeItems不包含“item”作为键。
{"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"
}
]};
$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';
}
}