1

i have an doubt. I want to check the new value is present in existing array which contains json data using PHP in_array method. I am explaining my code below.

{"introId":"582aa53755f5ab487614ddc9","introLabelName":"Age","isIntro":"Yes"}

Here i need to check 582aa53755f5ab487614ddc9 is present in the above array or not using PHP. Please help me.

1
  • IF your data is in JSON Format then you need to use json_decode() and then compare it with in_array(). Commented Nov 30, 2016 at 5:24

3 Answers 3

1
$json = '{"introId":"582aa53755f5ab487614ddc9","introLabelName":"Age","isIntro":"Yes"}';
$array = json_decode($json, true);
echo in_array('582aa53755f5ab487614ddc9', $array);

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this simple way:

   $json_array = '{"introId":"582aa53755f5ab487614ddc9","introLabelName":"Age","isIntro":"Yes"}';
$newvalue = '582aa53755f5ab487614ddc9';

$json_newArr = json_decode($json_array, true); 

if(in_array($newvalue, $json_newArr)){
    echo 'True';
}

Comments

0

Your given sample data is in JSON Format thats why you need to decode this using
json_decode() and then compare it with in_array().

$jsonData = '{"introId":"582aa53755f5ab487614ddc9","introLabelName":"Age","isIntro":"Yes"}';

$decodeArray = json_decode($jsonData, true);

if (in_array('582aa53755f5ab487614ddc9', $decodeArray)) {
    echo 'Found';
} else {
    echo 'Not Found';
}

Hope it should be solve your problem. Thank you.

Comments