Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Spiral's avatar

How can check if key exist in the array

I have a data in which some time raw not giving how can check if exist then

you can see my code

$request_url    = $item['request']['url']['raw'];
0 likes
9 replies
JonesRichard's avatar

The array_key_exists() function is used to check whether a specified key is present in an array or not. The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index.

1 like
Spiral's avatar

Thanks @jonesrichard for replying me.. Brother giving issue

ErrorException: array_key_exists() expects parameter 2 to be array, string given
if (array_key_exists('raw', $item['request']['url'])) {
                                $request_url    = $item['request']['url']['raw'];
                            }
Spiral's avatar

Thanks @michaloravec dear for replying to me...

the issue is that the raw key sometimes not giving

for example sometime giving only $item['request']['url']

MichalOravec's avatar
Level 75
$request_url = $item['request']['url']['raw'] ?? $item['request']['url'] ?? null;

if ($request_url) {
    //
}
2 likes
arsen-s's avatar

short case

$request_url = $item['request']['url']['raw'] ?? null
1 like

Please or to participate in this conversation.