Home  >  Article  >  Backend Development  >  PHP returns json Recommended methods for PHP to determine whether a JSON object exists

PHP returns json Recommended methods for PHP to determine whether a JSON object exists

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

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. Therefore, 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:

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

There will be an error 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 } 

And another way is to use isset to judge directly:

if(isset($structure['parts']))
{
} 
  //这个函数用来测试变量是否已经配置。若变量已存在则返回 true 值。其它情形返回 false 值。
  //因此需要若变量存在且值不为NULL,才返回 TURE

The above is the PHP judgment JSON object introduced by the editor Is there any method (recommended)? I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support of this website!

The above introduces the recommended method of PHP returning json to 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