CodeIgniter Forums
Strange bug, array returning different result after foreach on another var - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Strange bug, array returning different result after foreach on another var (/thread-45810.html)



Strange bug, array returning different result after foreach on another var - El Forum - 10-06-2011

[eluser]Costa Rica SEO[/eluser]
Ok, I'm closure compiling a group of JS files when I update my libraries. It worked fine until I added another file. Instead of seeing files A, B, and C, I see A, B, B. I used FirePHP to log out where my array was changing and to my surprise it was after a foreach on a completely unrelated variable. Like magic the array (that I didn't even touch) changes.

The original code (minus comments) was:
Code:
if(!empty($packages)){
            /* $this->data['external_js'] is normal */
            foreach($packages as $item){
                /* $this->data['external_js'] has changed */

I put logging in to see where the var changes, and you can see it below.
from: http://pastie.org/2647844

Code:
/*
* PHP 5.3.3 and 5.3.8 (Upgraded after finding this bug, still not working)
*
* fb_log() logs variables to FirePHP
*
* Problem: Building a list of JS to package in to one file. Missing one file, but getting two copies of another
* Test 1: Log the variable ($this->data['external_js']) and see where it's changing
* Result: Test one showed that $this->data['external_js']; was changing after the line foreach($packages as $item) immediately below it
* Test 2: Copy $this->data['external_js'] to $ext_js_for_firephp, which is not used anywhere else in the program
* Result: Both $ext_js_for_firephp and $this->data['external_js'] return altered
*
* Any Ideas?  @gmail I'm paul.dillinger
* http://profiles.google.com/paul.dillinger
*/

/* SECTION OF CODE WHERE PROBLEM IS */

        if(!empty($packages)){
            if($type=='js'){
                $ext_js_for_firephp = $this->data['external_js'];
                fb_log('$ext_js_for_firephp before', $ext_js_for_firephp);
            }

            foreach($packages as $item){
                if($type=='js'){
                    fb_log('$ext_js_for_firephp after', $ext_js_for_firephp);
                }

/* RESULT */

/* Before FirePHP returns:
$ext_js_for_firephp before = array(
    [0] => array(
        ['template_id'] => 30
        ['js_id'] => 9
        ['id'] => 9
        ['library_name'] => 'modernizr'
        ['file_name'] => 'modernizr.min.js'
        ['version_major'] => 2
        ['version_minor'] => 0
        ['version_build'] => 6
        ['static'] => 1
        ['package'] => 0
        ['footer'] => 0
        ['priority'] => 100
    )
    [1] => array(
        ['template_id'] => 30
        ['js_id'] => 12
        ['id'] => 12
        ['library_name'] => 'default'
        ['file_name'] => 'default.js'
        ['version_major'] => 0
        ['version_minor'] => 0
        ['version_build'] => 4
        ['static'] => 1
        ['package'] => 1
        ['footer'] => 0
        ['priority'] => 90
    )
    [2] => array(
        ['template_id'] => 37
        ['js_id'] => 11
        ['id'] => 11
        ['library_name'] => 'jquery-ui-custom'
        ['file_name'] => 'jquery-ui-1.8.11.custom.min.js'
        ['version_major'] => 1
        ['version_minor'] => 8
        ['version_build'] => 11
        ['static'] => 1
        ['package'] => 0
        ['footer'] => 0
        ['priority'] => 0
    )
)
*/

/* After FirePHP returns:
$ext_js_for_firephp after = array(
    [0] => array(
        ['template_id'] => 30
        ['js_id'] => 9
        ['id'] => 9
        ['library_name'] => 'modernizr'
        ['file_name'] => 'modernizr.min.js'
        ['version_major'] => 2
        ['version_minor'] => 0
        ['version_build'] => 6
        ['static'] => 1
        ['package'] => 0
        ['footer'] => 0
        ['priority'] => 100
    )
    [1] => array(
        ['template_id'] => 30
        ['js_id'] => 12
        ['id'] => 12
        ['library_name'] => 'default'
        ['file_name'] => 'default.js'
        ['version_major'] => 0
        ['version_minor'] => 0
        ['version_build'] => 4
        ['static'] => 1
        ['package'] => 1
        ['footer'] => 0
        ['priority'] => 90
    )
    [2] => array(
        ['template_id'] => 30
        ['js_id'] => 12
        ['id'] => 12
        ['library_name'] => 'default'
        ['file_name'] => 'default.js'
        ['version_major'] => 0
        ['version_minor'] => 0
        ['version_build'] => 4
        ['static'] => 1
        ['package'] => 1
        ['footer'] => 0
        ['priority'] => 90
    )
)
*/

Any ideas are appreciated...