Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用strpos查找对象中的特定单词_Php_Json - Fatal编程技术网

在php中使用strpos查找对象中的特定单词

在php中使用strpos查找对象中的特定单词,php,json,Php,Json,我正在尝试设置一个测试问题,在简短的答案中找到特定的单词。将答案标记为正确的单词将作为值存储在对象中。我试图找出如何使用strpos()实现这一点,但我提出的每一个备选方案都给了我一个空白屏幕 PHP: $myJSON=file_get_contents('quick.json'); $json=json_decode($myJSON); foreach($json作为$value){ foreach($value->回答为$index=>$options){ $findme=“应用程序”; $

我正在尝试设置一个测试问题,在简短的答案中找到特定的单词。将答案标记为正确的单词将作为值存储在对象中。我试图找出如何使用strpos()实现这一点,但我提出的每一个备选方案都给了我一个空白屏幕

PHP:

$myJSON=file_get_contents('quick.json');
$json=json_decode($myJSON);
foreach($json作为$value){
foreach($value->回答为$index=>$options){
$findme=“应用程序”;
$pos=strpos($options,$findme);
如果($pos==true){
echo$期权;
//回显$value->text->type->answer;
//echo($index.'.$options.
); //回波电流($值); } } } JSON: { “问题1”:{ “文本”:“DSA关注的两种类型的权限列表是什么?”, “类型”:“简短回答”, “答复”:{ “1”:“应用程序”, “2”:“行级别” } }, “问题2”:{ “文本”:“EmpowHR安全的基本要素是什么?”, “类型”:“简短回答”, “答复”:{ “1”:“权限列表” } }, “问题3”:{ “文本”:“炸弹是谁?”, “类型”:“简短回答”, “答复”:{ “1”:“许可” } } }
strpos返回找到的位置,如果未找到,则返回false

returnvalue==true:始终为false

returnvalue==true:如果未找到或在第一个位置(0)上找到,则为false;如果在第一个位置之后找到,则为true(所有数字!=0均为true)


返回值!==false:correct result

使用给定的JSON文件测试以下内容

重要提示:首先,我将
,true
添加到
$json=json\u decode($myJSON)
-->
$json=json\u解码($myJSON,true)这将obj转换为数组

在var_转储json编码后,我注意到您在试图解析的级别中混合了字符串和数组类型,因此使用
in_array()
过滤掉字符串,只遍历数组,并且能够在该obj中找到当前构建中“answers”部分的所有实例

$stmt = NULL;
$find = "application";
$myJSON = file_get_contents('quiz.json');
$json = json_decode($myJSON, true);

foreach( $json as $content ){
  foreach( $content as $target){
    if(is_array($target)){
      // we must find the key of the value within the next level of the array
      // and use it as index for the value $target to use in strpos() --> $target[$index]
      foreach($target as $index => $value){           
        if(strpos($target[$index], $find) !== false){
          $stmt = '<span>'.$target[$index].': CORRECT</span>';
        }
      }
    }    
  }
}

echo $stmt;
$stmt=NULL;
$find=“应用程序”;
$myJSON=file_get_contents('quick.json');
$json=json_decode($myJSON,true);
foreach($json作为$content){
foreach($content作为$target){
if(is_数组($target)){
//我们必须在数组的下一个级别中找到值的键
//并将其用作要在strps()中使用的值$target的索引-->$target[$index]
foreach($targetas$index=>$value){
if(strpos($target[$index],$find)!==false){
$stmt='.$target[$index]。:正确';
}
}
}    
}
}
echo$stmt;

这一点很不清楚。请举例说明结果应该是什么。另外,什么是
“应用程序”
以及为什么它是硬编码的?Op添加了JSON obj以显示quick.JSON中的内容。在我看来,这似乎是将obj解析为strps函数的问题。答案可能包括将json转换为数组,然后在循环中运行它。
if($pos==true)
。始终
false
。它要么是
false
要么是一个无符号整数,甚至可能是零。根据PHP手册中示例#1中的逻辑,我使用了if($pos==true)。感谢您的回复。但是,我将要合并的代码没有将其用作数组。我从您的语法中得到了一些想法,例如使用current(),因为我已经读到它可以迭代对象和数组。我试着用花括号代替方括号。不过,运气不好。
foreach( $json as $question=>$content ){
   foreach( $content as $key=>$value ){
      if( strpos( $value, 'applikation' ) !== false
         echo $value;
      }
   }
}
$stmt = NULL;
$find = "application";
$myJSON = file_get_contents('quiz.json');
$json = json_decode($myJSON, true);

foreach( $json as $content ){
  foreach( $content as $target){
    if(is_array($target)){
      // we must find the key of the value within the next level of the array
      // and use it as index for the value $target to use in strpos() --> $target[$index]
      foreach($target as $index => $value){           
        if(strpos($target[$index], $find) !== false){
          $stmt = '<span>'.$target[$index].': CORRECT</span>';
        }
      }
    }    
  }
}

echo $stmt;