Reputation:
Is there any way to set an array recursively? something along these lines:
$array = new ArrayElements ( ) ;
$array->set('index', 10)->set('indexInIndex',20);
and then the array would look like this:
Array
(
[index] => Array
(
[0] => 10
[indexInIndex] => 20
)
)
I know it must involve a fluid interface, but the gist of it is how to tell if a method was chained to a method 'set' and then take the index to the previous set setou, and write on top of this index
func_get_args
may also have a close and grab all the parameters and set in accordance with the thread, something like
public function set ( $index , $value ) {
if ( func_num_args() > 2 ) {
$indexes = array_splice ( func_get_args() , 2 ) ;
}
}
Then ..
->set( 'index' , 'value' , 'inIndex' , 'inIndex1' )
would be equal to: $storage [ 'inIndex' ] [ 'inIndex1' ] [ 'index' ] = 'value' ;
Upvotes: 0
Views: 90
Reputation: 31823
Your resulting array structures and argument orders seem odd to me. I wonder if if maybe your sample outputs are just mistakes. Maybe this helps you achieve your goal.
$r = array(array_pop($args));
foreach (array_reverse($indexes) as $index) {
$r = array($index => $r);
}
then
'a' , 'b' , 'c', 'val'
yields
$r['a']['b']['c'] = 'val';
Upvotes: 1