note 118254 added to function.array-reduce
| From: | directrix1 at gmail dot com | Date: | Tue, 03 Nov 2015 16:39:04 +0000 |
| Subject: | note 118254 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204080@lists.php.net to get a copy of this message | ||
So, if you were wondering how to use this where key and value are passed in to the function.
I've had success with the following (this example generates formatted html attributes from an
associative array of attribute => value pairs):
<?php
// Attribute List
$attribs = [
'name' => 'first_name',
'value' => 'Edward'
];
// Attribute string formatted for use inside HTML element
$formatted_attribs = array_reduce(
array_keys($attribs), // We pass in the array_keys instead of the
array here
function ($carry, $key) use ($attribs) { // ... then we 'use' the actual array
here
return $carry . ' ' . $key . '="' . htmlspecialchars(
$attribs[$key] ) . '"';
},
''
);
echo $formatted_attribs;
?>
This will output:
name="first_name" value="Edward"
----
Server IP: 72.52.91.14
Probable Submitter: 68.97.17.89
----
Manual Page -- http://php.net/manual/en/function.array-reduce.php
Edit -- https://master.php.net/note/edit/118254
Del: integrated -- https://master.php.net/note/delete/118254/integrated
Del: useless -- https://master.php.net/note/delete/118254/useless
Del: bad code -- https://master.php.net/note/delete/118254/bad+code
Del: spam -- https://master.php.net/note/delete/118254/spam
Del: non-english -- https://master.php.net/note/delete/118254/non-english
Del: in docs -- https://master.php.net/note/delete/118254/in+docs
Del: other reasons-- https://master.php.net/note/delete/118254
Reject -- https://master.php.net/note/reject/118254
Search -- https://master.php.net/manage/user-notes.php