Last active
December 3, 2017 09:13
-
-
Save Guibzs/ace88f98a227136f6a190458bda5c4c5 to your computer and use it in GitHub Desktop.
PHP ~ Returns a value within an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Function | |
function rgar( $array, $name ) | |
{ | |
if ( isset($array[$name]) ) | |
{ | |
return $array[$name]; | |
} | |
return ''; | |
} | |
## Example | |
$array = [ | |
'key' => 'value' | |
'key2' => 'value2' | |
]; | |
rgar( $array, 'key' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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