
Added by thebman220 on November-23-2008, 5:47 pm
<?php
/**
* @purpose : Joins an array containing any number of
* sub-arrays based on the parameters sent
* after $array
* @param array $array : The array to be joined. This variable must
* be an array, or a "Fatal, catchable error"
* will be triggered
* @param mixed * : Any parameters sent after $array will be
* used to join $array. It must be formatted
* as either a string or an array; if an array
* is sent, *[0] will used between elements,
* *[1] will be placed before each element, and
* *[2] will be placed after each element. If
* any of these values are omitted, their
* respective defaults will be used. If the
* number of nested arrays is greater than the
* number of extra arguments sent, a default
* will be used.
* @note : If too few arguments are sent, array_join
* will attempt to use the constant
* ARRAY_IMPLODE_DEFAULT_DELIM if it is set;
* otherwise, an empty string is used.
*/
function array_implode(array $array) {
$argv=func_get_args();
// Default joining deliminator
$def_delim=defined("ARRAY_IMPLODE_DEFAULT_DELIM")
?ARRAY_IMPLODE_DEFAULT_DELIM
:"";
// Get this joining's deliminator
$delim=isset($argv[1])
?$argv[1]
:$def_delim;
// Fix the value of $delim
if (!is_array($delim)) {
$delim=array(0=>"$delim");
} elseif (!isset($delim[0])) {
$delim[0]=$def_delim;
}
foreach (array(1,2) as $ind) {
if (!isset($delim[$ind])) {
$delim[$ind]="";
}
}
// Save other deliminators for recursive calls
$delims=array();
foreach (array_slice($argv,2,count($argv),true) as $i => $d) {
$delims[]="\$argv[$i]";
}
$delims=implode(",",$delims);
$delims=$delims==""?"":",$delims";
$strs=array();
foreach ($array as $val) {
$str=is_array($val)?eval("return array_implode(\$val$delims);"):$val;
$strs[]=$delim[1].$str.$delim[2];
}
return implode($delim[0],$strs);
}
?>
<?php
header("Content-type: text/plain");
$array=array(1,2,array(3,4,5),array(array(6,7),8),9);
print_r($array);
echo "\n".array_implode($array,array(" ","[","]"),"-");
define("ARRAY_IMPLODE_DEFAULT_DELIM","_");
echo "\n".array_implode($array,array(" ","[","]"),"-");
?>
Array
(
[0] => 1
[1] => 2
[2] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[3] => Array
(
[0] => Array
(
[0] => 6
[1] => 7
)
[1] => 8
)
[4] => 9
)
[1] [2] [3-4-5] [67-8] [9]
[1] [2] [3-4-5] [6_7-8] [9]
Added by thebman220 on May-2-2009, 4:40 am
Added by thebman220 on April-29-2009, 11:47 pm
Added by thebman220 on April-5-2009, 1:48 pm
Added by thebman220 on January-29-2009, 3:15 am
Added by thebman220 on January-29-2009, 3:04 am