EmperorJazzy Posted March 14, 2012 Share Posted March 14, 2012 So I'm reading a JSON, and I'm using the command GET_FILE_CONTENTS, and I'm getting results. However, the JSON is incomplete to a point where in some instances the variables/objects within do not always contain a certain array. Issue; I'm getting errors returned when that particular array does not exist. The code below is the line that's getting the error returned because the TAGS[0] doesn't exist in all instances. Question; How do I check if the array exists within the GET_FILE_CONTENTS object? See example; If($dall->tags[0]->name==$catgrp) Error: Notice: Undefined offset: 0 in /var/www/vhosts/website/httpdocs/testjson/index.php on line 63 Notice: Trying to get property of non-object in /var/www/vhosts/website/httpdocs/testjson/index.php on line 63 Link to comment https://forums.phpfreaks.com/topic/258890-object-checking-get_file_contents/ Share on other sites More sharing options...
Muddy_Funster Posted March 14, 2012 Share Posted March 14, 2012 wrap it in a check for the array first, before trying to access the contents of it: If($dall->tags){ //do your thing } Link to comment https://forums.phpfreaks.com/topic/258890-object-checking-get_file_contents/#findComment-1327203 Share on other sites More sharing options...
trq Posted March 14, 2012 Share Posted March 14, 2012 property_exists. Link to comment https://forums.phpfreaks.com/topic/258890-object-checking-get_file_contents/#findComment-1327204 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.