get object data in array object php code example
Example 1: php find object by property name in array of objects
$customOptions = [
{
userLabel: 'Check out my awesome label',
userName: 'Non-Stop Code Shop'
},
{
userColor: '#2680eb',
userFont: 'comic_sans'
}
];
function findObjectPropertyByName($propName, $arrayOfObjects)
{
$array = array_filter($arrayOfObjects, function ($obj) use (&$propName) {
return array_key_exists('NotificationBody', get_object_vars($obj));
});
if (!empty($array)) {
return $array[0]->$propName;
}
return null;
}
$userFont = findObjectPropertyByName('userFont', $customOptions);
Example 2: php use variable as object key
$nid_property = 'my_nid';
$nid = $row->{$nid_property};