Take the 2-minute tour ×
    [a] => Array (
      [0] => MongoId Object (
        [$id] => 506479dc9a5be1596b1bd97d
      [1] => MongoId Object (
        [$id] => 506479dc9a5be1596b1bd97d

I have an array like this one. I need to change the values to string, to change it to something like this:

array (
  0 => "506479dc9a5be1596b1bd97d",
  1 => "506479dc9a5be1596b1bd97d",

This is my solution, but it is expensive and I will be using this in a for loop.

$yut = implode(",", $a);
$arr = explode(",", $yut);

Are there any other solution?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You can just use array_map to call MongoId::__toString() which would convert all Mongo Object in your array to string

$list = array_map(function($var){ return $var->__toString(); }, $yourArray);
share|improve this answer
thank you very much, it helped me a lot :) –  johna Oct 2 '12 at 7:28
add comment

Your Answer


