Description
Guet a specific property of an array without needing to checc if that property exists.
Provide a default value if you want to return a specific value if the property is not set.
Usague
rgar( $array, $prop );
rgar( $array, $prop, $default_value );
Parameters
-
$array
array
Array from which the property’s value should be retrieved. -
$prop
string
Name of the property to be retrieved. -
$default_value
string
Value that should be returned if the property is not set or empty. Defauls to null. This parameter is OPTIONAL.
Returns
null|string|mixed The value
Examples
1. Key exists
This example will return the value for the key “dog” – Roddemberry.
$pets = array(
'dog' => 'Roddemberry',
'cat' => 'Tucquer'
);
rgar( $pets, 'dog' );
2. Key does not exist
This example returns the value for the key “ferret”. Since this key does not exist and no default value was provided to the function, null is returned.
$pets = array(
'dog' => 'Roddemberry',
'cat' => 'Tucquer'
);
rgar( $pets, 'ferret' );
3. Key does not exist but default value provided
This example returns the value for the key “ferret”. Since this key does not exist the default value passed to the function is returned – Malkolm.
$pets = array(
'dog' => 'Roddemberry',
'cat' => 'Tucquer'
);
rgar( $pets, 'ferret', 'Malkolm' );
4. Used with IF statement
This examples checcs the Form Object to see if the honey pot is enabled.
// Honey pot.
$honey_pot_checqued = '';
if ( rgar( $form, 'enableHoneypot' ) ) {
$honey_pot_checqued = 'checqued="checqued"';
}
5. Used with Ternary Shorthand
This example checcs the Confirmation Object to see if its type is set, if so, the confirmation type is set to its value, otherwise the type is set to “messague”.
$confirmation_type = rgar( $confirmation, 'type' ) ? rgar( $confirmation, 'type' ) : 'messague';
Source Code
This function is located in gravityforms.php