note 93459 modified in function.array-map by thiago
| From: | thiago@php.net | Date: | Thu, 10 Sep 2009 19:35:37 +0000 |
| Subject: | note 93459 modified in function.array-map by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160434@lists.php.net to get a copy of this message | ||
I recently discovered that PHP has no loop-free manner for modifying the keys in an array. I was
hoping to use a built-in function to prefix the keys.
Having found no native function to do this, I threw together this monster:
<?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
--was--
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
http://php.net/manual/en/function.array-map.php