note 124942 added to function.implode
| From: | HonkderHase@php.net | Date: | Fri, 24 Apr 2020 17:43:39 +0000 |
| Subject: | note 124942 added to function.implode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214531@lists.php.net to get a copy of this message | ||
If you want to implode an array as key-value pairs, this method comes in handy.
The third parameter is the symbol to be used between key and value.
<?php
function mapped_implode($glue, $array, $symbol = '=') {
return implode($glue, array_map(
function($k, $v) use($symbol) {
return $k . $symbol . $v;
},
array_keys($array),
array_values($array)
)
);
}
$arr = [
'x'=> 5,
'y'=> 7,
'z'=> 99,
'hello' => 'World',
7 => 'Foo',
];
echo mapped_implode(', ', $arr, ' is ');
// output: x is 5, y is 7, z is 99, hello is World, 7 is Foo
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 185.22.143.180)
----
Manual Page -- http://php.net/manual/en/function.implode.php
Edit -- https://master.php.net/note/edit/124942
Del: integrated -- https://master.php.net/note/delete/124942/integrated
Del: useless -- https://master.php.net/note/delete/124942/useless
Del: bad code -- https://master.php.net/note/delete/124942/bad+code
Del: spam -- https://master.php.net/note/delete/124942/spam
Del: non-english -- https://master.php.net/note/delete/124942/non-english
Del: in docs -- https://master.php.net/note/delete/124942/in+docs
Del: other reasons-- https://master.php.net/note/delete/124942
Reject -- https://master.php.net/note/reject/124942
Search -- https://master.php.net/manage/user-notes.php