0

I'm trying to modify the value of my array which is itself in a linked list Sounds like:

  $z = new SplDoublyLinkedList();

  $z->push(array('Hello', 0));
  $z->push(array('world', 4));

  $p = & $z->offsetGet(1);                // reference ?

  $p[0]='change';                         // indirection like ? 
  $p[1]=5;


  $q = &$z->offsetGet(1);                 // element of my array remains "world", 4

But it doesn't work. Of course, If I push class object, it works...

Is there a way to have the same behaviour with array() ? ... Obviously and according to SplDoublyLinkedList declaration, I can't... :(

3
  • You know your code triggers a warning ~ "PHP Strict Standards: Only variables should be assigned by reference" Commented May 13, 2014 at 6:34
  • Do you also have warning? I have "Strict standards: Only variables should be assigned by reference" for line 8 and 14 Commented May 13, 2014 at 6:34
  • 3
    you would have to roll your own setter, because arrayaccess.offsetset doesn't set by reference. Commented May 13, 2014 at 6:34

1 Answer 1

0

Use the offsetSet() method

  $z = new SplDoublyLinkedList();

  $z->push('Hello');
  $z->push('world');

  $z->offsetSet(0, 'change');
  $z->offsetSet(1, 5);


  $q = $z->offsetGet(1);

I don't think this class accepts arrays as the input type for push.

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.