php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32524 Advanced imploding
Submitted: 2005-04-01 01:11 UTC Modified: 2016-03-27 07:24 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bart at mediawave dot nl Assigned: krakjoe (profile)
Status: Closed Package: *General Issues
PHP Version: 5CVS-2005-04-01 (dev) OS: Any
Private report: No CVE-ID: None
 [2005-04-01 01:11 UTC] 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'"




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-28 00:07 UTC] 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.
 [2016-03-27 07:24 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: krakjoe
 [2016-03-27 07:24 UTC] krakjoe@php.net
What you said was the most elegant solution, is how things work today.

Sorry about the wait :)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 02 18:01:28 2025 UTC