本文介绍了如何在 PHP 中将数组元素转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个包含对象的数组:
If I have a array with objects:
$a = array($objA, $objB);
(每个对象都有一个__toString()
-方法)
(each object has a __toString()
-method)
如何将所有数组元素转换为字符串,以便数组 $a
不包含更多对象,而是包含它们的字符串表示?是单行还是我必须手动遍历数组?
How can I cast all array elements to string so that array $a
contains no more objects but their string representation? Is there a one-liner or do I have to manually loop through the array?
推荐答案
单行:
$a = array_map('strval', $a);
// strval is a callback function
请参阅 PHP 文档:
See PHP DOCS:
这篇关于如何在 PHP 中将数组元素转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!