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
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.
If your goal is output your array to a string for debbuging: you can use the print_r() function, which receives an expression parameter (your array), and an optional boolean return parameter. Normally the function is used to echo the array, but if you set the return parameter as true, it will return the array impression.
Example:
//We create a 2-dimension Array as an example
$ProductsArray = array();
$row_array['Qty'] = 20;
$row_array['Product'] = "Cars";
array_push($ProductsArray,$row_array);
$row_array2['Qty'] = 30;
$row_array2['Product'] = "Wheels";
array_push($ProductsArray,$row_array2);
//We save the Array impression into a variable using the print_r function
$ArrayString = print_r($ProductsArray, 1);
//You can echo the string
echo $ArrayString;
//or Log the string into a Log file
$date = date("Y-m-d H:i:s", time());
$LogFile = "Log.txt";
$fh = fopen($LogFile, 'a') or die("can't open file");
$stringData = "--".$date."\n".$ArrayString."\n";
fwrite($fh, $stringData);
fclose($fh);
This will be the output:
Array
(
[0] => Array
(
[Qty] => 20
[Product] => Cars
)
[1] => Array
(
[Qty] => 30
[Product] => Wheels
)
)
For completeness and simplicity sake, the following should be fine:
$str = var_export($array, true);
It does the job quite well in my case, but others seem to have issues with whitespace. In that case, the answer from ucefkh
provides a workaround from a comment in the PHP manual.
Since the issue of whitespace only comes up when exporting arrays, you can use the original var_export() for all other variable types. This function does the job, and, from the outside, works the same as var_export().
<?php
function var_export_min($var, $return = false) {
if (is_array($var)) {
$toImplode = array();
foreach ($var as $key => $value) {
$toImplode[] = var_export($key, true).'=>'.var_export_min($value, true);
}
$code = 'array('.implode(',', $toImplode).')';
if ($return) return $code;
else echo $code;
} else {
return var_export($var, $return);
}
}
?>
http://www.php.net/manual/en/function.var-export.php#54440
Convert array to a string in PHP:
Use the PHP join
function like this:
$my_array = array(4,1,8);
print_r($my_array);
Array
(
[0] => 4
[1] => 1
[2] => 8
)
$result_string = join(',' , $my_array);
echo $result_string;
Which delimits the items in the array by comma into a string:
4,1,8
implode
or join
(they're the exact same thing) would work here. Alternatively, you can just call array_pop
and get the value of the only element in the array.