note 24957 modified in function.explode by vincent
| From: | vincent@php.net | Date: | Mon, 23 Feb 2004 10:46:39 +0000 |
| Subject: | note 24957 modified in function.explode by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65580@lists.php.net to get a copy of this message | ||
While explode() and implode() are helpful functions, I found I needed to write additional functions
to correctly handle two dimensional arrays. Here are the two functions:
// this is a replacement for the internal explode function so that
// strings representing 2D arrays (ie matrices) maybe made again
// into their array format
function double_explode($first_break,$second_break,$string)
{
// creates array based on the first division character
$array = explode($first_break, $string);
// loops through the created array in search of any strings
// that need to be converted to arrays
for ($k=0; $k < count($array); $k++)
{
// the number of times the second division character
// is present
$count = substr_count($array[$k], $second_break);
// if the second division character is present; ie,
// if the string needs to be converted to an array
if ($count>0)
{
// creates array from string and puts it in
// the main array where the string was
$array[$k] = explode($second_break, $array[$k]);
}
}
return $array;
}
// just as double_explode functions akin to explode, so too does
// double_implode to implode
function double_implode($first_break, $second_break, $array)
{
$string = "";
$k=0;
$num_length = count($array);
foreach ($array as $key => $value)
{
// if the element of the main array is an array
if (is_array($array[$key]))
{
// append the string of the converted arary to
// the string being constructed of the whole
// main array
$string .= implode($second_break, $array[$key]);
}
else
{
// append the element to the string
$string .= $array[$key];
}
$k++;
// append the delimiting character to the string
$string .= $first_break;
}
return $string;
}
I imagine that there may be more efficient ways to do this; any examples would be welcome. I hope
that someone finds this helpful.
--was--
While explode() and implode() are helpful functions, I found I needed to write additional functions
to correctly handle two dimensional arrays. Here are the two functions:
// this is a replacement for the internal explode function so that
// strings representing 2D arrays (ie matrices) maybe made again
// into their array format
function double_explode($first_break,$second_break,$string)
{
// creates array based on the first division character
$array = explode($first_break, $string);
// loops through the created array in search of any strings
// that need to be converted to arrays
for ($k=0; $k < count($array); $k++)
{
// the number of times the second division character
// is present
$count = substr_count($array[$k], $second_break);
// if the second division character is present; ie,
// if the string needs to be converted to an array
if ($count>0)
{
// creates array from string and puts it in
// the main array where the string was
$array[$k] = explode($second_break, $array[$k]);
}
}
return $array2;
}
// just as double_explode functions akin to explode, so too does
// double_implode to implode
function double_implode($first_break, $second_break, $array)
{
$string = "";
$k=0;
$num_length = count($array);
foreach ($array as $key => $value)
{
// if the element of the main array is an array
if (is_array($array[$key]))
{
// append the string of the converted arary to
// the string being constructed of the whole
// main array
$string .= implode($second_break, $array[$key]);
}
else
{
// append the element to the string
$string .= $array[$key];
}
$k++;
// append the delimiting character to the string
$string .= $first_break;
}
return $string;
}
I imagine that there may be more efficient ways to do this; any examples would be welcome. I hope
that someone finds this helpful.
http://www.php.net/manual/en/function.explode.php