Home >Backend Development >PHP Tutorial >Use PHP to determine whether a JSON object exists
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!