Note that there are some explanatory texts on larger screens.

plurals
  1. POIgnorance OR bug on PHP's "foreach" construct?
    primarykey
    data
    text
    <p>I have a dataset obtained from MySQL that goes like this:</p> <pre> Array ( [0] => Array ( [views] => 14 [timestamp] => 06/04 [views_scaled] => 4.9295774647887 [unix_time] => 1239022177 ) [1] => Array ( [views] => 1 [timestamp] => 19/04 [views_scaled] => 0.35211267605634 [unix_time] => 1240194544 ) ... ... ... ) 1 </pre> <p>(it's post-processed, 'timestamp' was really a timestamp before, but that doesn't matter anyways)</p> <p>The array is stored on <code>$results</code>, and in the middle of my code I do something like this:</p> <pre> $results = array_merge($results, $new_days); $a = $results; foreach ($results as $row) { $unix_time[] = $row['unix_time']; } $b = $results; </pre> <p><b>The problem:</b> <code>$a</code> and <code>$b</code> are both different. The first one shows the array as it's supposed to, and the second one has the same <code>count()</code>, but it's fourth element is a duplicate from the last one. As far as I know, I'm not passing anything by reference, so <code>$results</code> Isn't meant to change (maybe the pointer, but not it's content). I'm using PHP 5.2.4 on Mac OS X 10.5.2.</p> <p><b>The obvious question:</b> Is this somehow the intended behavior, a bug or I'm doing something wrong here? (not a boolean answer please ;) <hr /> <b>EDIT:</b> Thank you all for the interest, I don't know exactly how much extra code should I post, I don't do much before except for retrieving the data from the DB and a <code>foreach</code> to parse the timestamp and build a new array (<code>$new_days</code>) for the missing days. This is all working fine.</p> <p>This code goes after the one I've posted early:</p> <pre> array_multisort($unix_time, SORT_ASC, $results); $days = implode('|', array_pluck('timestamp', $results)); $views = implode('|', array_pluck('views', $results)); $views_scaled = implode(',', array_pluck('views_scaled', $results)); </pre> <p>(<code>array_pluck()</code> is a custom function to generate an array from a column in a typical DB-dumped dataset) <hr /> <b>EDIT 2:</b> Thanks again, here's <a href="http://snipplr.com/view/14259/ignorance-or-bug-on-phps-foreach-construct/" rel="nofollow noreferrer">the full snippet</a> and the output from the <code>$results</code> array <a href="http://snipplr.com/view/14257/" rel="nofollow noreferrer"><code>$a</code></a> and <a href="http://snipplr.com/view/14258/" rel="nofollow noreferrer"><code>$b</code></a> (also referenced in the code's comments).</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.
 

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