php - Handling !isset values without checking isset -
i have object pulled api similar structure:
class stdclass#544 (5) {   public $id =>   string(1) "4"   public $name =>   class stdclass#545 (6) {     public $en =>     string(6) "test"   }   public $description =>   class stdclass#546 (6) {     public $en =>     string(20) "my description."   } } however, api inconsistent. sometimes, objects have no description attributes:
class stdclass#544 (5) {   public $id =>   string(1) "7"   public $name =>   class stdclass#545 (6) {     public $en =>     string(6) "another test"   } } as i'm looping through api, i'm mapping values objects array:
$values = array(); foreach( $objects $object ) {   $values = array(     'id' => $object->id,     'name' => $object->name->en,     'description' => $object->description->en,   );   somefunction($values); } however, since description not set/existing attribute of object, throws exception if description missing.
what best approach handle mapping of attributes not there? can check existence of every single attribute before assigning them:
$values = array(); if ( isset($object->id) )   $values['id'] = $object->id;  if ( isset($object->name) , isset($object->name->en) )   $values['id'] = $object->name->en;  if ( isset($object->description) , isset($object->description->en) )   $values['description'] = $object->description->en; but approach becomes cumbersome , more difficult maintain, when there more attributes listed. above example simplified version of problem. in reality there many more attributes. manually checking existence of attributes becomes more of pain, , more when nested attributes.
one simple solution i've found use @ symbol, forces php ignore exceptions:
$values = array(); foreach( $objects $object ) {   @$values = array(     'id' => $object->id,     'name' => $object->name->en,     'description' => $object->description->en,   );   somefunction($values); } this results in description being null (which ideal me). no exception thrown. i'm pretty sure @ used debugging purposes , don't think it's proper use in way.
what best approach handling this? can somehow set values in array null if attribute/key doesn't exist in object without having manually check existence of every single attribute?
you define keys want extract object in array , extract each key in foreach loop.
edit
just noticed accessing objects within objects. if need go 1 level deep example below should it.
if value you're extracting property of object additional level deep, make corresponding key array of keyname => subkeyname.
$a = new stdclass(); $a->id = 'hello'; $a->description = 'world'; $a->name = new stdclass(); $a->name->en = 'name';  $keys = ['id','description','notset',['name' => 'en']]; $value = []; foreach($keys $key) {      if(is_array($key)) {         $subkey = current($key);         $key = key($key);         $value[$key] = (isset($a->$key->$subkey))           ? $a->$key->$subkey : null;     }     else {         $value[$key] = (isset($a->$key))            ? $a->$key : null;     } }  var_dump($value); 
Comments
Post a Comment