note 107797 added to function.array-keys
| From: | qeremy | Date: | Mon, 05 Mar 2012 17:50:44 +0000 |
| Subject: | note 107797 added to function.array-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186503@lists.php.net to get a copy of this message | ||
Simple ways to prefixing arrays;
<?php
function array_keys_prefix($arr, $pref = "") {
$rarr = array();
foreach ($arr as $key => $val) {
$rarr[$pref.$key] = $val;
}
return $rarr;
}
function array_keys_prefix_multi($arr, $pref = "") {
$rarr = array();
foreach ($arr as $key => $val) {
$rarr[] = array_keys_prefix($val, $pref);
}
return $rarr;
}
// $a = array("foo" => "FOO", "bar" => "BAR",
"baz" => "BAZ"); // or
$a = array("foo" => "FOO", "bar" => "BAR",
"baz" => array(1,2,3));
print_r(array_keys_prefix($a, "my_"));
// db fetch...
$products = array(
array("id" => 1, "name" => "Foo"),
array("id" => 2, "name" => "Bar")
);
print_r(array_keys_prefix_multi($products, "product_"));
?>
Array
(
[my_foo] => FOO
[my_bar] => BAR
[my_baz] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Array
(
[0] => Array
(
[product_id] => 1
[product_name] => Foo
)
[1] => Array
(
[product_id] => 2
[product_name] => Bar
)
)
----
Server IP: 69.147.83.197
Probable Submitter: 194.27.149.90
----
Manual Page -- http://www.php.net/manual/en/function.array-keys.php
Edit -- https://master.php.net/note/edit/107797
Del: integrated -- https://master.php.net/note/delete/107797/integrated
Del: useless -- https://master.php.net/note/delete/107797/useless
Del: bad code -- https://master.php.net/note/delete/107797/bad+code
Del: spam -- https://master.php.net/note/delete/107797/spam
Del: non-english -- https://master.php.net/note/delete/107797/non-english
Del: in docs -- https://master.php.net/note/delete/107797/in+docs
Del: other reasons-- https://master.php.net/note/delete/107797
Reject -- https://master.php.net/note/reject/107797
Search -- https://master.php.net/manage/user-notes.php