0

I have the following code:

var_dump($cursor);
foreach($cursor as $obj) {
    echo "<div class='item' id='" . $obj['_id'] . "'>";
        echo "<span class='listnick'>" . $obj['nick'] . "</span>";
    echo "</div>";
}

The result of var_dump is the following:

array(2) {
  [0]=>
  &array(9) {
    ["_id"]=>
    object(MongoId)#9 (1) {
      ["$id"]=>
      string(24) "50af8dcd9cc231534400000c"
    }

    ["nick"]=>
    string(6) "safari"

  }
  [1]=>
  array(9) {
    ["_id"]=>
    object(MongoId)#8 (1) {
      ["$id"]=>
      string(24) "50af8dca9cc2315644000009"
    }
    ["nick"]=>
    string(6) "chrome"
  }
}

so obviously the foreach should print out "safari" and "chrome", but the problem is really weird:

Sometimes it returns "safari" twice and omits "chrome", and viceversa for the other client. I tried putting the var_dump and the foreach loop near to be sure they are the SAME and there are no changes in the object between the two commands, but really I got no idea what's going on.

Any help? Thanks in advance.

5
  • wow this is interesting, how did you manage to output this &array(9) { . I am referring to & ( the reference). can you give the code where you fill the $cursor array? i can't seem to recreate this output, no matter what and how i try to set the referrence. Commented Nov 23, 2012 at 15:26
  • foreach($cursor as &$obj) { $obj['whatever'] = "foo"; } Apparently, this is what modified it to be like this. Commented Nov 23, 2012 at 15:28
  • haven't thought of that 10x :) Commented Nov 23, 2012 at 15:29
  • do you know if there is a way to alter the array without incurring into this issue? Commented Nov 23, 2012 at 15:34
  • well it depends on what you want to do. Commented Nov 24, 2012 at 9:37

2 Answers 2

1

Notice how safari is a reference to an array: &array.

This might result from having a foreach where $obj is a reference:

foreach($cursor as &$obj) {
   ..
}
//unset($obj);

In PHP, the scope of $obj does not end with execution of the loop, so you should do an unset whenever you looped using a reference.

This might also result from using the reference assignment somewhere:

$cursor[] =& $safari;
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, yes. I previously edited the array with this: foreach($cursor as &$obj) { ... }. Is there a way to "safely" edit it so that this does not happen?
in other words, do you know if there is a way to alter the array without incurring into this issue?
Yes, just unset($obj) after each loop, especially the ones using references.
0

This are 2 difference codes ... One is using & reference which would modify the array output and the other is not

array(2) {
  [0]=>
  &array(9) {
  ^----------------------------- Reference sign 
    ["_id"]=>
    object(MongoId)#9 (1) {
      ["$id"]=>
      string(24) "50af8dcd9cc231534400000c"
    }

    ["nick"]=>
    string(6) "safari"

  }

Topical example of what happened

$a = $b = array(array("_id" => new MongoId(),"nick" => "chrome"));

foreach ( $a as $k => &$v )
    $k == "nick" and $v['nick'] = "Safari";

foreach ( $b as $k => $v )
    $k == "nick" and $v['nick'] = "Safari";

var_dump($a);
var_dump($b);

Output

array (size=1)
  0 => 
    &array (size=2)
      '_id' => 
        object(MongoId)[1]
          public '$id' => string '50af93a2a5d4ff5015000011' (length=24)
      'nick' => string 'Safari' (length=6) <------ changed

array (size=1)
  0 => 
    array (size=2)
      '_id' => 
        object(MongoId)[2]
          public '$id' => string '50af93a2a5d4ff5015000012' (length=24)
      'nick' => string 'chrome' (length=6) <------- not changed 

Can you see that one if the Nick is modified not the two

2 Comments

do you know if there is a way to alter the array without incurring into this issue?
You can use the key to identify the exact one you want to alter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.