Home  >  Article  >  Backend Development  >  PHP returns json PHP method to determine whether a json object exists

PHP returns json PHP method to determine whether a json object exists

WBOY
WBOYOriginal
2016-07-28 08:27:531461browse

In the actual test, when PHP reads the json array and uses simple if or array_key_exists to determine whether the object exists, an error will be reported. The following is the correct judgment method from Google search

In fact, the error is reported because I am not very proficient in PHP. Maybe the correct judgment method I think is not the most perfect solution or even wrong. This blog post is reserved for my own use. Error code:

<span>$structure</span> = <span>imap_fetchstructure</span>(<span>$connection</span>, <span>$id</span>,<span> FT_UID);
</span><span>if</span> (<span>array_key_exists</span>('parts', <span>$structure</span><span>))
{
}</span>

An error will appear Warning: array_key_exists() expects parameter 2 to be array, boolean Given

The correct solution is:

<span>if</span> (<span>is_array</span>(<span>$structure</span>) && <span>array_key_exists</span>('parts', <span>$structure</span>)) <br>{ <span>//</span><span>...magic stuff here <br>}</span>

And another way is to use isset to directly judge:

<span>if</span>(<span>isset</span>(<span>$structure</span>['parts'<span>]))
 {
 }<br></span>
   //This function is used to test whether the variable has been configured. Returns true if the variable already exists. Otherwise a false value is returned.

  // Therefore, it is necessary to return TURE

only if the variable exists and the value is not NULL

The above introduces the method for PHP to return json and PHP to determine whether a json object exists, including the content of PHP returning json. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn