note 125308 added to function.implode
| From: | info at ensostudio dot ru | Date: | Fri, 28 Aug 2020 17:32:01 +0000 |
| Subject: | note 125308 added to function.implode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215040@lists.php.net to get a copy of this message | ||
<?php
* Join pieces with a string recursively.
*
* @param mixed $glue String between pairs(glue) or an array pair's glue and key/value glue or
$pieces.
* @param iterable $pieces Pieces to implode (optional).
* @return string Joined string
*/
function double_implode($glue, iterable $pieces = null): string
{
$glue2 = null;
if ($pieces === null) {
$pieces = $glue;
$glue = '';
} elseif (is_array($glue)) {
list($glue, $glue2) = $glue;
}
$result = [];
foreach ($pieces as $key => $value) {
$result[] = $glue2 === null ? $value : $key . $glue2 . $value;
}
return implode($glue, $result);
}
?>
Examples:
<?php
$array = ['a' => 1, 'b' => 2];
$str = implode($array);
$str = implode(', ', $array);
$str = implode(['" ', '="'], $array);
$iterator = new ArrayIterator($array);
$str = implode($iterator);
$str = implode(', ', $iterator);
$str = implode(['" ', '="'], $iterator);
?>
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.166.36 (proxied: 31.129.215.166)
----
Manual Page -- http://php.net/manual/en/function.implode.php
Edit -- https://master.php.net/note/edit/125308
Del: integrated -- https://master.php.net/note/delete/125308/integrated
Del: useless -- https://master.php.net/note/delete/125308/useless
Del: bad code -- https://master.php.net/note/delete/125308/bad+code
Del: spam -- https://master.php.net/note/delete/125308/spam
Del: non-english -- https://master.php.net/note/delete/125308/non-english
Del: in docs -- https://master.php.net/note/delete/125308/in+docs
Del: other reasons-- https://master.php.net/note/delete/125308
Reject -- https://master.php.net/note/reject/125308
Search -- https://master.php.net/manage/user-notes.php