rgar()

Description

Get a specific property of an array without needing to check if that property exists.

Provide a default value if you want to return a specific value if the property is not set.

Usage

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. Defaults 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” – Roddenberry.

$pets = array(
   'dog' => 'Roddenberry',
   'cat' => 'Tucker'
);
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' => 'Roddenberry',
   'cat' => 'Tucker'
);
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 – Malcolm.

$pets = array(
   'dog' => 'Roddenberry',
   'cat' => 'Tucker'
);
rgar( $pets, 'ferret', 'Malcolm' );

4. Used with IF statement

This examples checks the Form Object to see if the honey pot is enabled.

// Honey pot.
$honey_pot_checked = '';
if ( rgar( $form, 'enableHoneypot' ) ) {
  $honey_pot_checked = 'checked="checked"';
}

5. Used with Ternary Shorthand

This example checks 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 “message”.

$confirmation_type = rgar( $confirmation, 'type' ) ? rgar( $confirmation, 'type' ) : 'message';

Source Code

This function is located in gravityforms.php