ID: 32524
Comment by: c dot duvergier dot div at online dot fr
Reported By: bart at mediawave dot nl
Status: Open
Bug Type: Feature/Change Request
Operating System: Any
PHP Version: 5CVS-2005-04-01 (dev)
New Comment:
My suggestion to solve that problem is the following (I was going to
open a new bug, but since you already did it, let's continue here) :
I would suggest to modify implode() so that a callback function can be
specified.
Example:
--------
$pieces = array('A', 'suggestion');
echo implode_callback(' | ', $pieces, 'crc32');
Description of need:
--------------------
Actually, implode(string $glue, array $pieces) uses __toString()
function on each element of $pieces before glueing them together.
It would be usefull to have the possibility to call a user-defined
function instead (such as data verification, string transformation).
Could be done by doing an array_walk() on $pieces and then an implode
on the modified array, but that implies to traverse the array twice.
In #40097, Derick suggested a bogus solution :
array_walk(implode('-', $string), 'callbackFunc');
(doesn't work as first array_walk() parameter must be an array.
Still it doesn't fix performance issue.
Previous Comments:
------------------------------------------------------------------------
[2005-04-01 01:11:23] bart at mediawave dot nl
Description:
------------
I often find myself being in the situation where I need more advanced
ways to implode() something. For example, a way to implode an array
into:
"table1 AS alias1, table2 AS alias2, table3 AS alias3"
or:
"var1=value1&var2=value2&var3=value3"
I would like to make 3 suggestions to implement such a functionality in
PHP.
1) Extend implode so that it can handle a second (or more?) "glue"
argument.
$vars = array(
'var1' => 'value1',
'var2' => 'value2',
'var3' => 'value3'
);
or maybe it would need a multidimensional array:
$vars = array(
array('var1', 'value1'),
array('var2', 'value2'),
array('var3', 'value3')
);
echo implode($vars, '=', '&');
// Would print: var1=value1&var2=value2&var3=value3
The problem here is that implode would need to get its arguments in a
different order. Perhaps, this could be solved by creating a new
function.
2) Modify array_map() so that, when the third argument is a string, it
is always passed along as an extra argument to all the function calls:
$vars = array(
array('var1', 'value1'),
array('var2', 'value2'),
array('var3', 'value3')
);
echo implode('&', array_map('implode', $vars, '='));
// Would print: var1=value1&var2=value2&var3=value3
3) This is probably the most powerfull and elegant solution. (My
personal favourite) Modify the __toString() magic method so that, when
an object is passed to a function that needs a string as input,
__toString() is called. In this example __toString() could be something
like:
function __toString() {
return $this->name.'='.$this->value;
}
And the implode() code could look like:
$vars = array(
new urlVar('var1', 'value1'),
new urlVar('var1', 'value1'),
new urlVar('var1', 'value1')
);
echo implode($vars, '&');
// Would print: var1=value1&var2=value2&var3=value3
This could also produce more complex strings like:
"WHERE var1='value1' AND var2='value2' OR var3='value3'"
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=32524&edit=1