Convert the First Element of an Array to a String in PHP

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

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.

array with one value converted to string?

you could use:

current($tab);
array_shift($tab); // if you don't need to use the array for anything afterwards

Id question why it's in an array in the first place if you don't need it to be and what the issue with using $tab[0] is

How to convert an array to a string in PHP?

I would turn it into CSV form, like so:

$string_version = implode(',', $original_array)

You can turn it back by doing:

$destination_array = explode(',', $string_version)

Get the first element of an array

Original answer, but costly (O(n)):

array_shift(array_values($array));

In O(1):

array_pop(array_reverse($array));

Other use cases, etc...

If modifying (in the sense of resetting array pointers) of $array is not a problem, you might use:

reset($array);

This should be theoretically more efficient, if a array "copy" is needed:

array_shift(array_slice($array, 0, 1));

With PHP 5.4+ (but might cause an index error if empty):

array_values($array)[0];

convert array into array to string is possible..?

You can use array_column() and implode() function to do this.

Here is how you can do it,

$values=array_column($array,"country_id");
$country_string=implode($values);

array_column() returns the values from a single column of the input,
identified by the column_key(country_id).

implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between
each element.


implode() can have two arguments, first as glue by which you want to join the elements and second as the array of elements. If first argument is not given then default glue is ",".

Convert single-element array into string

I use this, to optimize single element arrays from SimpleXmlElement:

function optimize( $config )
{
foreach ( $config as $key => $value )
if( is_array( $value ) && count( $value ) == 1 && isset( $value[0] ))
$config[$key] = $value[0];

return $config
}

As single array elements can be nested some levels deep, you can use this function as a recursive function.

How to cast array elements to strings in PHP?

A one-liner:

$a = array_map('strval', $a);
// strval is a callback function

See PHP DOCS:

array_map

strval

php get string from array elements and concatenate into new array element

You need array_push()

In your case

<?php
$arr2 = [];
foreach ($matches as $match) {
$values = explode(" ", $match);
array_push($arr2, $values[0], $values[1], $values[2]);
}

var_dump($arr2); // now $arr2 should contain all values
?>


Related Topics



Leave a reply



Submit