Php determines whether a json object exists, and php judges a json object

Source: Internet
Author: User

Php determines whether a json object exists, and php judges a json object

In the actual test, when php reads a json array, if or array_key_exists is used to determine whether an object exists, an error is returned. The following is a correct method for google searching.

In fact, when an error is reported, I am not very familiar with php, so I may think that the correct judgment method is not the perfect solution or even the wrong one. This blog post is for your own use.

Error code:

$ Structure = imap_fetchstructure ($ connection, $ id, FT_UID); if (array_key_exists ('parts', $ structure )){}

The following error occurs: Warning: array_key_exists () expects parameter 2 to be array, boolean given

The correct solution is:

If (is_array ($ structure) & array_key_exists ('parts', $ structure ))
{//... Magic stuff here
}

Another method is to use isset for direct judgment:

If (isset ($ structure ['parts']) {}

// This function is used to test whether the variable has been configured. If the variable already exists, true is returned. Otherwise, false is returned.

// If the variable exists and the value is not NULL, true is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.