note 87405 deleted from ref.array by danbrown
| From: | danbrown@php.net | Date: | Thu, 04 Dec 2008 03:35:25 +0000 |
| Subject: | note 87405 deleted from ref.array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147607@lists.php.net to get a copy of this message | ||
Note Submitter: cristiano at galvao dot pro dot br
----
Hi! I use this function to convert an array into a string.
Note that to convert back into array (flat array), we can use standard PHP split function.
Here is:
<?php
/*--- Function ARRAY TO STRING ---
*
* Description: Turns an array into a concatenated string
* Arguments: Flat or multidimensional array and optional separator
* Example: (array(1,2,3,array('x','yz'),4),"-") --->
"1-2-3-x-yz-4"
*
* Author: Cristiano Galvão
* Contact: cristiano@galvao.pro.br
* About: http://www.galvao.pro.br
*
*/
function array_to_string( $array, $separator = NULL ) {
if( is_array( $array ) ) {
foreach( $array as $element ) {
$string .= array_to_string( $element, $separator ) . $separator;
}
$string = substr( $string, 0, strlen( $string ) - strlen( $separator ) );
} else {
$string = $array;
}
return $string;
}
?>