note 93459 added to function.array-map
| From: | macnimble at gmail dot com | Date: | Thu, 10 Sep 2009 19:27:49 +0000 |
| Subject: | note 93459 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160432@lists.php.net to get a copy of this message | ||
A little modification to my previous function. This one allows for unlimited arrays, but note that
the parameter order has been changed.
<?php
function array_prefix_keys ()
{
$args = func_get_args();
$prefix = array_shift($args);
$func = create_function('$p,$k','return "$p{$k}";');
foreach ($args AS $key => $array):
$args[$key] = array_combine( array_map( $func
, array_fill(0,count($array),$prefix)
, array_keys($array)
)
, array_values($array)
);
endforeach;
return $args;
}
$array1 = array(
'id' => 1,
'title' => 'about-us',
'author' => 'Bill Brown',
'dated' => '2009-SEP-15'
);
$array2 = array(
'id' => 2,
'title' => 'about-them',
'author' => 'Bill Smith',
'dated' => '2010-SEP-15'
);
echo '<pre>',print_r($array1,1),print_r($array2,1),'</pre>';
$array = array_prefix_keys("content_",$array1,$array2);
$array1 = $array[0];
$array2 = $array[1];
echo '<pre>',print_r($array1,1),print_r($array2,1),'</pre>';
?>
This will produce the following output:
Array
(
[id] => 1
[title] => about-us
[author] => Bill Brown
[dated] => 2009-SEP-15
)
Array
(
[id] => 2
[title] => about-them
[author] => Bill Smith
[dated] => 2010-SEP-15
)
Array
(
[content_id] => 1
[content_title] => about-us
[content_author] => Bill Brown
[content_dated] => 2009-SEP-15
)
Array
(
[content_id] => 2
[content_title] => about-them
[content_author] => Bill Smith
[content_dated] => 2010-SEP-15
)
Hope it helps!
Bill
----
Server IP: 209.41.74.194
Probable Submitter: 98.235.179.75
----
Manual Page -- http://www.php.net/manual/en/function.array-map.php
Edit -- https://master.php.net/note/edit/93459
Del: integrated -- https://master.php.net/note/delete/93459/integrated
Del: useless -- https://master.php.net/note/delete/93459/useless
Del: bad code -- https://master.php.net/note/delete/93459/bad+code
Del: spam -- https://master.php.net/note/delete/93459/spam
Del: non-english -- https://master.php.net/note/delete/93459/non-english
Del: in docs -- https://master.php.net/note/delete/93459/in+docs
Del: other reasons-- https://master.php.net/note/delete/93459
Reject -- https://master.php.net/note/reject/93459
Search -- https://master.php.net/manage/user-notes.php