note 81215 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:19:47 +0000 |
| Subject: | note 81215 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189749@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
So I looked through all the problem solutions posted here, and combined them into one nice
recursive function.
<?php
function implode_md( $glue, $array, $key = NULL, $list = NULL )
{
if( !is_array( $array ) )
return $array;
if( !sizeof( $array ) )
return "";
if( !is_null( $key ) )
{
if( strpos( $key, "." ) )
{
$keys = array_reverse( explode( ".", $key ) );
$currentKey = array_pop( $keys );
if( sizeof( $keys ) ) $keys = implode( ".", $keys );
else $keys = implode( "", $keys );
}
else
{
$currentKey = $key;
$keys = NULL;
}
if( array_key_exists( $currentKey, $array ) )
return implode_md( $glue, $array[$currentKey], $keys, $list );
else
return "";
}
if( !empty( $list ) )
{
$last = array_pop( $array );
if( count( $array ) )
return implode_md( $glue, $array, $key ) . " $list $last";
else
return $last;
}
$ret = array();
for( $i = 0; $i < sizeof( $array ); $i++ )
if( is_array( $array[$i] ) ) $ret[] = implode_md( $glue, $array[$i], $key, $list );
else $ret[] = $array[$i];
return implode( $glue, $ret );
}
?>