dd($result); ian array like below.But it returns false for the check in_array('resourceId',$result).Can someone tell me why it behaves so?
array:1 [
"resourceId" => "Etkl6g-zSB7EpP-Da1zN619mWbo8UZGl1R467hyNsftAO3as_gtcNJeFMgY63hjE2tfT_Wbt-PQ8FB9cmhV6mPPMCmvbDQMSFRQuif2zhlz8kTdJhUNUX1Vp6aXSLQ4LOPWZ8-ncRXILvjnrbJmjFiCx9Z4hi_eNIC-MF7uQxvgEZX_OYfwMgKiba5gSXYSnaubB4qqo7hKJBbR-TfdAeGW0S22sP3pLI99aX55gLU5DS8eC_Gb6Gfz-99rDTjvlZxfpfaCXU6C0VHfMl3vcTg5UqmVU9agtas3yHOILu7qBDMdTfVK8J"
]
CodePudding user response:
in_array() looks for a value and not the key.
Use something like
if (isset($result['resourceId')))
which checks if there is a key with that name.
There is also some useful info on What's quicker and better to determine if an array key exists in PHP? about various aspects of checking if a key exists.
CodePudding user response:
in_array() check if associative array has value. to search for key existence use php array_key_exists('resourceId', $result).
Also using \Illuminate\Support\Arr::has($result, 'resourceId') is more laravel like manner.
CodePudding user response:
As others have suggested isset() and array_key_exists(), keep in mind that:
isset()will returnfalse- If the key doesn't exist
- If it exists, but it is set to
null
array_key_exists()will returnfalse- If the key doesn't exist
(Sorry, I did not see @apokryfos' comment until I posted this, credits to @apokryfos)
CodePudding user response:
You can check it next way
if (!empty(array_keys($result, 'resourceId'))) {
// logic
}
