Totsy
Totsy

Reputation: 13

Find a specific word in an object using strpos in php

I'm trying to set up a test question that will find certain words in a short answer. The words, which will mark the answer as correct, will be stored as values in an object. I was trying to figure out how to do it with strpos(), but every alternative that I come up with gives me a blank screen.

PHP:

$myJSON = file_get_contents('quiz.json');
$json = json_decode($myJSON); 
foreach($json as $value) {
    foreach($value->answer as $index => $options) {
        $findme = "application";
        $pos = strpos($options, $findme);
        if ($pos === true) {
            echo $options;
    //echo $value->text->type->answer;
     //echo ($index. ' '. $options . '<br>');
        //echo current($value);
        }
    }

}

JSON:

{

    "question1": {
        "text": "What are the two types of permission lists that DSA concentrates on?",
        "type": "short_answer",
        "answer": {
                "1": "application", 
                "2": "row-level"
        }
    },
    "question2": {
        "text": "What are the building blocks for EmpowHR Security?",
        "type": "short_answer",
        "answer": {
                "1": "permission lists"
        }
    },
    "question3": {
        "text": "Who is the bomb?",
        "type": "short_answer",
        "answer": {
                "1": "permission"
        }
    }       
}

Upvotes: 0

Views: 443

Answers (2)

dale landry
dale landry

Reputation: 8610

Tested the following using your given JSON file.

Important: First I added the , true to $json = json_decode($myJSON); --> $json = json_decode($myJSON, true); This turns the obj into an array

After var_dumping the json encoded I noticed you had mixed string and array types in the level you were trying to parse, so used in_array() to filter out the strings and only iterate through the arrays and was able to locate all instances of the "answers" section in its current build within that obj.

$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;

Upvotes: 2

daddykom
daddykom

Reputation: 220

foreach( $json as $question=>$content ){
   foreach( $content as $key=>$value ){
      if( strpos( $value, 'applikation' ) !== false
         echo $value;
      }
   }
}

strpos returns the found position and false if not found.

returnvalue === true: allways false

returnvalue == true: false if not found or found on first position (0), true if found after first position (all numbers != 0 are true)

returnvalue !== false: correct result

Upvotes: 1

Related Questions