2

Can i use array_key_exists to check for a given key into a bidimensional array ? or there is a better way to check for ['payment']['id'] key ?

I try : array_key_exists('payment.id',$data) and array_key_exists('product.id',$data)

But can not see it working as expected.

Eg. array_key_exists(payment.id,$data), where i have an array :
array(
[payment] array( [id]=>123 [date]=>2016-01-20 ) 
[product] array( [id]=>456 [qtty]=>3 )
)
1
  • 1
    you can use isset(data['payment']['id']) Commented Jan 20, 2016 at 13:39

1 Answer 1

3

Try this. Give this a read.

if (array_key_exists('id', $data['payment'])) {
    echo "The 'id' element is in the payment array.";
}
if (array_key_exists('id', $data['product'])) {
    echo "The 'id' element is in the product array.";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Great work´s just fine, Thank you! i can access any level of an array dimension ?
You're welcome! Yep, you can access any level of the array as long as you're providing proper parameters to the array_key_exists function.
Yes! i check array_key_exists('id', $data[$key]['product'])) , and work´s fine!

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.