将数组值转换为字符串 [英] converting array values to string

查看:44
本文介绍了将数组值转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


    [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循环中使用它.

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

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

还有其他解决方法吗?

推荐答案

您可以只使用array_map调用 MongoId :: __ toString()会将数组中的所有Mongo对象转换为字符串

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);

这篇关于将数组值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆