0

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' ;

3
  • Your set method is behaving inconsistently. Also, whats wrong with just using $array['index']['foo'] or $array['index'] = array(10) Commented Dec 7, 2011 at 14:19
  • no, I'm just looking at whether there is any way to do this by using methods Commented Dec 7, 2011 at 14:20
  • 1
    It probably is. But it's ugly and no one with a sane mind would want to use it, so why bother? Do it the regular and standard way. Keep it simple. Commented Dec 7, 2011 at 14:22

1 Answer 1

1

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';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.