Convert the first element of an array to a string in PHP

后端 未结 13 1753
盖世英雄少女心
盖世英雄少女心 2020-12-05 02:00

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

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 02:48

    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.

     '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.

提交回复
热议问题