1

I am trying to create a function (unless one already exists?) which is supposed to accept an "unlimited" amount of arguments (using func_get_args()) to find the value of an array key. Regardless of how deep within the array it is to be found.

Say if I would use $registry->getSetting( 'template', 'default' ); I am supposed to get the value of $this->properties['settings']['template']['default'] or if I would you $registry->getSetting( 'users', 1, 'name', 'first' ); I would expect it to return the value of $this->properties['users'][1]['name']['first'] (just a second example with a couple of extra arguments).

Now, to do something like this, I could count the amount of arguments passed using func_num_args() and then do a switch with different cases. Although, this would limit it to a certain amount of keys.

So I am asking you if there is a way of doing this, to allow an "unlimited" amount rather than a fixed amount of arguments to access a deeper key of an array.

<?PHP class Registry { // Values are actually fetched from a config file, but for easier understanding private $properties = array( 'settings' => array( 'template' => array( 'default' => 'default', 'caching' => TRUE ) ) ); public function getSetting( ) { // do something like // return $this->properties['settings'][func_get_args( )]; } } ?> 

Any help whatsoever is highly appreciated.
Thank you!

5 Answers 5

2
<?PHP class Registry { // Values are actually fetched from a config file, but for easier understanding private $properties = array( 'settings' => array( 'template' => array( 'default' => 'default', 'caching' => TRUE ) ) ); public function getSetting() { $result = $this->properties; foreach (func_get_args() as $arg) { if (isset($result[$arg])) { $result = $result[$arg]; } else { return; } } return $result; } } $r = new Registry(); var_dump($r->getSetting('settings', 'template', 'default')); ?> 
Sign up to request clarification or add additional context in comments.

Comments

1

Hmm, I'm not sure if this is what you want, but this is straight from the PHP manual:

Variable-length argument lists

PHP 4 and above has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions.

No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal.

I would just loop over the arguments and output them into a single array (I might be misinterpreting your question):

$output = array(); $arguments = func_get_args(); foreach ($argument as $arg) { $output[$arg] = $this->properties['settings'][$arg]; } return $output; 

Good luck?

1 Comment

Yes it was indeed, thank you very much. I will however accept zerkms post as he was first, thank you once again though!
1

I don't know if there is a PHP function which can do this, but my way is working fine too:

class Registry { // Values are actually fetched from a config file, but for easier understanding private $properties = array( 'settings' => array( 'template' => array( 'default' => 'default', 'caching' => TRUE ) ) ); public function getSetting( $array = array() ) { $return = $this->properties['settings']; foreach( $array as $a ) { $return = $return[$a]; } return $return; } } 

then use:

$Registry = new Registry(); $template = $Registry->getSetting(array( 'template' ) ); $template_caching = $Registry->getSetting(array( 'template', 'caching' ) ); 

Comments

0

Use dot as separator for nested arrays:

Registry::getAttribute('first.second.1.value'); // will return $ar['first']['second'][1]['value'] 

In getAttribute explode param by . and map it into registry values.

1 Comment

This would leave me with the same issue though, however it has already been solved. Thank you anyway!
0

Something like this (untested):

function getSetting ( /* .. */ ) { $args = func_get_args(); array_unshift( $args, $this->properties ); return getSettingInternal( $args ); } function getSettingInternal ( $args ) { $arr = $args[0]; if ( count( $args ) > 1 ) { $key = array_slice( $args, 1, 1 ); if ( !is_array( $arr ) ) return $arr; $arr = array_key_exists( $arr, $key ) ? $arr[$key] : $arr['default']; $args[0] = $arr; return getSettingInternal( $args ); } else return $arr; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.