Skip to content

Instantly share code, notes, and snippets.

@Guibzs
Last active December 3, 2017 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guibzs/ace88f98a227136f6a190458bda5c4c5 to your computer and use it in GitHub Desktop.
Save Guibzs/ace88f98a227136f6a190458bda5c4c5 to your computer and use it in GitHub Desktop.
PHP ~ Returns a value within an array
## Function
function rgar( $array, $name )
{
if ( isset($array[$name]) )
{
return $array[$name];
}
return '';
}
## Example
$array = [
'key' => 'value'
'key2' => 'value2'
];
rgar( $array, 'key' );
## Function
function rgars( $array, $name )
{
$names = explode( '/', $name );
$val = $array;
foreach ( $names as $current_name )
{
$val = rgar( $val, $current_name );
}
return $val;
}
## Example
$array = [
'firstlevel' => [
'secondlevel' => [
'thirdlevel' => 'value'
]
]
];
rgars( $array, 'firstlevel/secondlevel/thirdlevel' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment