3
    [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?

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1
$new_array = array_map('strval', $array);

strval is php built in function that returns string value

like

function ($value){
   return (string)$value;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.