数组的第一个元素转换为PHP字符串 [英] Convert the first element of an array to a string in PHP

查看:106
本文介绍了数组的第一个元素转换为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 or implode, 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屋!

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