Ignorance OR bug on PHP's “foreach” construct?

By : lima
Source: Stackoverflow.com
Question!

I have a dataset obtained from MySQL that goes like this:

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

(it's post-processed, 'timestamp' was really a timestamp before, but that doesn't matter anyways)

The array is stored on $results, and in the middle of my code I do something like this:

$results = array_merge($results, $new_days);
$a = $results;
foreach ($results as $row)
{
    $unix_time[] = $row['unix_time'];
}
$b = $results;

The problem: $a and $b are both different. The first one shows the array as it's supposed to, and the second one has the same count(), 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 $results 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.

The obvious question: Is this somehow the intended behavior, a bug or I'm doing something wrong here? (not a boolean answer please ;)


EDIT: 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 foreach to parse the timestamp and build a new array ($new_days) for the missing days. This is all working fine.

This code goes after the one I've posted early:

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));

(array_pluck() is a custom function to generate an array from a column in a typical DB-dumped dataset)


EDIT 2: Thanks again, here's the full snippet and the output from the $results array $a and $b (also referenced in the code's comments).

By : lima


Answers

Unless I'm mistaken, this was a PHP bug a while back. I don't know the details, but arrays and references have been screwed up for a little bit.

By : Evert


Inspecting your code snippet, really quickly (just about to leave the office for the day), it is probably to do with something passing by reference in your (first) loop. Try using normal by value and just storing everything into a fresh result array. (will remove any mysteries that could be going on). Could also try making the second $row in the second foreach a different name.. beats me - can't tell you with really looking at this more.

also this line and following block of code won't execute

if ($last_day != $day_before_this_one AND $last_day)

could have something to do with it, new days will never fill up and the merge could be doing something funky.

Wouldn't call this an answer but its a start to look at I guess

By : Louis


This video can help you solving your question :)
By: admin