0

How can compare each a from content json_encode that inserted in database with a string as shorthand code without use loop? (this values was checkbox that inserted in database with json_encode)

Example

$json_encode = ["how", "are", "hello", "what"];

echo ($json_encode == 'hello') ? 'It is true' :'';
1
  • 1
    Can you clarify, I've read the question a few times but Im afraid it isnt too clear what you specifically want Commented Oct 6, 2011 at 9:45

2 Answers 2

1

The code is a little bit of a 'round a bout' way of doing things but this should do the trick:

$json_encode = '["how", "are", "hello", "what"]';    
echo ( in_array('hello', json_decode($json_encode)) ? 'It is true' :'' );

Your initial $json_encode isnt setup correctly as a proper JSON string, and required decoding to use the array checking functionality later on.

A better approach maybe:

$json_string = json_encode(array("how", "are", "hello", "what"));
echo ( in_array('hello', json_decode($json_string )) ? 'It is true' :'' );
Sign up to request clarification or add additional context in comments.

Comments

0

try with in_array() function:

$json_encode = ["how", "are", "hello", "what"];

echo ( in_array('hello', $json_encode) ? 'It is true' :'' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.