Note that there are some explanatory texts on larger screens.

plurals
  1. POReference vs. value copy for PHP arrays?
    primarykey
    data
    text
    <p>I am missing something obvious. Here's a trivial piece of PHP, littered with debug echos:</p> <pre><code>function echo_rows(&amp;$res) { $rows= array(); while ($row= $res-&gt;fetch()) { echo $row['ccorID'] . "\r\n"; $rows[]= $row; echo $rows[0]['ccorID'] . "\r\n"; } echo "---.---\r\n"; echo count($rows) . "\r\n"; foreach($rows as $row) { echo $row['ccorID'] . "\r\n"; } echo json_encode($rows); } </code></pre> <p>Here's what I see in the response:</p> <pre><code>0 0 3 3 13 13 182 182 ---.--- 4 182 182 182 182 </code></pre> <p>It seems perfectly clear to me that :-</p> <ul> <li>I have 4 rows of data</li> <li><code>$rows[]= $row;</code> does NOT perform a value copy, despite what it says here <a href="http://uk.php.net/manual/en/language.types.array.php" rel="nofollow noreferrer">http://uk.php.net/manual/en/language.types.array.php</a></li> </ul> <p>Anyone got any idea what I need to do to get a copy of $row (which is an associative array)?</p> <p>Thanks.</p> <p>EDIT: Since many of you are so insistent to know what $res is, here is the class. I genuinely believe that this is more likely to confuse than enlighten (hence the omission from my OP).</p> <pre><code>class mysqlie_results { private $stmt; private $paramArray= array(); private $assocArray= array(); public function __construct(&amp;$stmt) { $this-&gt;stmt= $stmt; $meta= $stmt-&gt;result_metadata(); while ($colData= $meta-&gt;fetch_field()) { $this-&gt;paramArray[]= &amp;$this-&gt;assocArray[$colData-&gt;name]; } call_user_func_array(array($stmt,'bind_result'),$this-&gt;paramArray); $meta-&gt;close(); } public function __destruct() { $this-&gt;stmt-&gt;free_result(); } public function fetch() { return $this-&gt;stmt-&gt;fetch()? $this-&gt;assocArray : false; } } </code></pre>
    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.
 

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