I have an array and want to convert it to string. I know I can use join, implode but in my case array have only one item. So why do I use join or implode as the combine array values into a single string with specified separator.

Note that array key is not always 18.

This array is the output of a PHP function which returns array only.

Array(18 => 'Somthing');

Do I really needs to use implode, join?

Is there any other way to convert that array into string ?

Comments

It depends. What do you want as the resulting string given the array above?

Written by shinkou

Accepted Answer

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.

Written by Berry Langerak
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki