0

I have a array and within that array i have more elements. I want to check if a specific element exist

  $jsondata = file_get_contents('php://input');
  $jsonobj = json_decode($jsondata, true);

Possibility 1

$jsonobj=Array(
      [user] => Array
            (
              [userId] => 1501148600220
              [locale] => en-US
                // no TOKEN
            )
);

Possibility 2

$jsonobj=Array(
          [user] => Array
                (
                  [userId] => 1501148600220
                  [locale] => en-US
                  [token] => 12345    // TOKEN exist
                )
               );

The follwing is a decoded JSON.
The Received JSON sometimes might and might not contain the element 'token'.

I want to check if the $jsonobj has the element 'token'

    if (array_key_exists('token', $jsonobj)) {
    echo "Element is in the array";
    }

How do i check this according to the structure of my array?

UPDATE

Sometimes, the jsonobj might not contain anything. It could be empty. If it is empty i get an error

PHP Warning:  array_key_exists() expects parameter 2 to be array
1
  • Well, you obviously decode the JSON string to have an object or array and then check if that element or property exists. What is the question here? Commented Jul 27, 2017 at 10:20

5 Answers 5

3

You just need to pass the index 'user' in the multidimensional $jsonobj array

Like this

if (array_key_exists('token', $jsonobj['user'])) {
  echo "Element is in the array";
 }

Modified:-

$jsonobj=json_decode($jsonobj,true);
if(!empty($jsonobj['user'])&&is_array($jsonobj['user']))
{
    if (array_key_exists('token', $jsonobj['user'])) {
        echo "Element is in the array";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get an error, PHP Warning: array_key_exists() expects parameter 2 to be array
Try the modified answer
3
 if (array_key_exists('token', $jsonobj['user'])) {
  echo "Element is in the array";
 }

2 Comments

I get an error, PHP Warning: array_key_exists() expects parameter 2 to be array
Yes. It was the empty array case. Just saw that someone sorted that out. Nice
1

You have to check your $jsonobj array key that is user. So, if you want to check user array key then you should write like this,

if (array_key_exists('token', $jsonobj['user'])) 
{
  echo "Element is in the array";
}

See this Manual.

Comments

1
foreach($jsonobj as $obj => $val){
    if(is_array($val)){     
        if(array_key_exists('token', $val)){
            echo 'exist';
        }else{
            echo 'not exist';
        }
    }
}

Comments

0

You can always use this one liner to check if nested key exists in array:

Arr::has($jsonobj, 'user.token');
// Or
Arr::has($jsonobj, ['user', 'token']);

If you want to learn more about this method or other methods useful when dealing with multidimensional arrays check out this php array library

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.