Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>edit: Using object was only a guess. It turns out that the problem is caused by references as others have guessed before.</p> <p>Since we don't know (yet) what $res is and what <code>$res-&gt;fetch()</code> actually returns let me try to replicate the behaviour.</p> <pre><code>&lt;?php $res = new Foo; echo_rows($res); function echo_rows(&amp;$res) { // as provided in the question } class Foo extends ArrayObject { protected $bar, $counter; public function fetch() { if ( ++$this-&gt;counter &gt; 4 ) { return false; } else { $this-&gt;bar['ccorID'] = $this-&gt;counter; return $this-&gt;bar; } } public function __construct() { $this-&gt;bar = new ArrayObject; $this-&gt;counter = 0; } } </code></pre> <p>prints</p> <pre><code>1 1 2 2 3 3 4 4 ---.--- 4 4 4 4 4 [{"ccorID":4},{"ccorID":4},{"ccorID":4},{"ccorID":4}] </code></pre> <p>The reason is, that all elements in $rows point to the same underlying object (see <a href="http://docs.php.net/language.oop5.references" rel="nofollow noreferrer">http://docs.php.net/language.oop5.references</a>) since my class Foo always returns the same object and just modifies the state of this single object.</p> <hr> <p>update: "what might the manual mean when it says "Use the reference operator to copy an array by reference"?"</p> <p>Let's start with this script</p> <pre><code>&lt;?php $arr = array(); $s = ''; $b = array(); $b['foo'] = $arr; $b['bar'] = $s; $b['foo']['bar'] = 1; $b['bar'] = 'xyz'; var_dump($arr, $s); </code></pre> <p>the output is</p> <pre><code>array(0) { } string(0) "" </code></pre> <p>i.e. <code>$b['foo']['bar'] = 1;</code> and <code>$b['bar'] = 'xyz';</code> didn't change $arr and $s since the array contains copies of the values.</p> <p>Now change the two assignments to use references</p> <pre><code>$b['foo'] = &amp;$arr; $b['bar'] = &amp;$s; </code></pre> <p>and the output changes to </p> <pre><code>array(1) { ["bar"]=&gt; int(1) } string(3) "xyz" </code></pre> <p>i.e. the elements are not copies.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload