note 128387 added to function.array-column
| From: | opencart dot ocfilter at gmail dot com | Date: | Wed, 12 Apr 2023 15:40:31 +0000 |
| Subject: | note 128387 added to function.array-column | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220343@lists.php.net to get a copy of this message | ||
Array multiple columns:
<?php
function array_columns() {
$args = func_get_args();
$array = array_shift($args);
if (!$args) {
return $array;
}
$keys = array_flip($args);
return array_map(function($element) use($keys) {
return array_intersect_key($element, $keys);
}, $array);
}
?>
EXAMPLE:
<?php
$products = [
[
'id' => 2,
'name' => 'Phone',
'price' => 210.3
],
[
'id' => 3,
'name' => 'Laptop',
'price' => 430.12
]
];
print_r(array_columns($products, 'name', 'price'));
?>
Output:
Array
(
[0] => Array
(
[name] => Phone
[price] => 210.3
)
[1] => Array
(
[name] => Laptop
[price] => 430.12
)
)
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 46.33.33.2)
----
Manual Page -- https://php.net/manual/en/function.array-column.php
Edit -- https://main.php.net/note/edit/128387
Del: integrated -- https://main.php.net/note/delete/128387/integrated
Del: useless -- https://main.php.net/note/delete/128387/useless
Del: bad code -- https://main.php.net/note/delete/128387/bad+code
Del: spam -- https://main.php.net/note/delete/128387/spam
Del: non-english -- https://main.php.net/note/delete/128387/non-english
Del: in docs -- https://main.php.net/note/delete/128387/in+docs
Del: other reasons-- https://main.php.net/note/delete/128387
Reject -- https://main.php.net/note/reject/128387
Search -- https://main.php.net/manage/user-notes.php