Convert array of MongoId objects to an array of strings
·
Answer a question
\[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?
Answers
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);
更多推荐
所有评论(0)