From:             bart at mediawave dot nl
Operating system: Any
PHP version:      5CVS-2005-04-01 (dev)
PHP Bug Type:     Feature/Change Request
Bug description:  Advanced imploding

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 bug report at http://bugs.php.net/?id=32524&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=32524&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=32524&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=32524&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=32524&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=32524&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=32524&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=32524&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=32524&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=32524&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=32524&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=32524&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=32524&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=32524&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=32524&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=32524&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=32524&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=32524&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=32524&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=32524&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=32524&r=mysqlcfg

Reply via email to