gpt4 book ai didi

php - 将 MongoId 对象数组转换为字符串数组

转载 作者:可可西里 更新时间:2023-11-01 09:28:09 27 4
gpt4 key购买 nike

    [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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com