Home  >  Article  >  Backend Development  >  Use PHP to determine whether a JSON object exists

Use PHP to determine whether a JSON object exists

不言
不言Original
2018-06-07 14:19:393474browse

This article mainly introduces the relevant information on the method (recommended) of PHP to determine whether a JSON object exists. It is very good and has reference value. Friends in need can refer to it

Read in php during the actual test When taking a json array, using a simple if or array_key_exists to determine whether the object exists will result in an error. The following is the correct judgment method found on Google

In fact, an error occurs 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 your 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 there is another way to use isset to directly judge:

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

The above is this article That’s all. Thank you for reading. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

JQuery and PHP implement dynamic progress bar upload display

The above is the detailed content of Use PHP to determine whether a JSON object exists. For more information, please follow other related articles on the PHP Chinese website!

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