我有一个 PHP 数组,想将其转换为字符串。
我知道我可以使用 join
或 implode
,但在我的例子中,数组只有一项。为什么我必须在只有一项的数组中使用组合值?
这个数组是我的 PHP 函数的输出,它返回一个数组:
Array(18 => 'Something');
如何将其转换为字符串?
最佳答案
Is there any other way to convert that array into string ?
您不想将数组转换为字符串,您想要获取数组唯一元素的值,如果我没看错的话。
<?php
$foo = array( 18 => 'Something' );
$value = array_shift( $foo );
echo $value; // 'Something'.
?>
使用 array_shift 你不必担心索引。
编辑:请注意,array_shift 不是唯一返回单个值的函数。 array_pop()、current()、end()、reset(),它们都会返回一个元素。所有已发布的解决方案均有效。但是,使用数组移位,您可以确保只会获得数组的第一个 值,即使有多个值也是如此。
关于php - 在 PHP 中将数组的第一个元素转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6262186/