[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",
)
这是我的解决方案,但它很昂贵,我将在 for
循环中使用它。
$yut = implode(",", $a);
$arr = explode(",", $yut);
还有其他解决办法吗?
最佳答案
您可以只使用array_map
来调用MongoId::__toString()这会将数组中的所有 Mongo 对象转换为字符串
$list = array_map(function($var){ return $var->__toString(); }, $yourArray);
关于php - 将 MongoId 对象数组转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682232/