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... :(

Stef
  • 3,691
  • 6
  • 43
  • 58
  • You know your code triggers a warning ~ *"PHP Strict Standards: Only variables should be assigned by reference"* – Phil May 13 '14 at 06:34
  • Do you also have warning? I have "Strict standards: Only variables should be assigned by reference" for line 8 and 14 – Marcin Nabiałek May 13 '14 at 06:34
  • 3
    you would have to roll your [own setter](http://stackoverflow.com/questions/8685186/arrayaccess-in-php-assigning-to-offset-by-reference#answer-8685304), because [arrayaccess.offsetset](http://php.net/manual/en/class.arrayaccess.php) doesn't set by reference. – pce May 13 '14 at 06:34

1 Answers1

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.

user3587554
  • 353
  • 1
  • 7