数组的第一个元素转换为PHP字符串 [英] Convert the first element of an array to a string in PHP
问题描述
我有一个PHP数组,并想将其转换为字符串。
我知道我可以使用加入
或破灭
,但对我来说阵列只有一个项目。为什么我不得不使用一个阵列,只有一个项目组合价值?
这阵是我的PHP函数的返回数组的输出:
阵列(18 =>'东西');
我如何将其转换为字符串?
是否有任何其他的方式来该数组转换成字符串?
块引用>您不想数组转换为字符串,你想要得到的数组的唯一元素的值,如果我正确读取。
< PHP
$富=阵列(18 =>'东西');
$值= array_shift($ foo的);
回声$价值; //'东西'。?>使用array_shift你不必担心指数。
的修改的:你要知道,array_shift是不是会返回一个值的唯一功能。 array_pop(),电流(),结束(),复位(),它们都将返回一个单一的元件。所有发布的解决方案的工作。使用数组转变不过,你可以肯定,你会永远只能得到的第一个的数组的值,即使在有多个。
I have a PHP array and want to convert it to a string.
I know I can use
join
orimplode
, but in my case array has only one item. Why do I have to use combine values in an array with only one item?This array is the output of my PHP function which returns an array:
Array(18 => 'Something');
How do I convert this to a string?
解决方案Is there any other way to convert that array into string ?
You don't want to convert the array to a string, you want to get the value of the array's sole element, if I read it correctly.
<?php $foo = array( 18 => 'Something' ); $value = array_shift( $foo ); echo $value; // 'Something'. ?>
Using array_shift you don't have to worry about the index.
EDIT: Mind you, array_shift is not the only function that will return a single value. array_pop( ), current( ), end( ), reset( ), they will all return that one single element. All of the posted solutions work. Using array shift though, you can be sure that you'll only ever get the first value of the array, even when there are multiple.
这篇关于数组的第一个元素转换为PHP字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!