Req #32524 [Opn->Csd]: Advanced imploding
| From: | krakjoe@php.net | Date: | Sun, 27 Mar 2016 07:24:24 +0000 |
| Subject: | Req #32524 [Opn->Csd]: Advanced imploding | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-200150@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=32524&edit=1
ID: 32524
Updated by: krakjoe@php.net
Reported by: bart at mediawave dot nl
Summary: Advanced imploding
-Status: Open
+Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Any
PHP Version: 5CVS-2005-04-01 (dev)
-Assigned To:
+Assigned To: krakjoe
Block user comment: N
Private report: N
New Comment:
What you said was the most elegant solution, is how things work today.
Sorry about the wait :)
Previous Comments:
------------------------------------------------------------------------
[2008-01-28 00:07:46] c dot duvergier dot div at online dot fr
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.
------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=32524&edit=1