PHP Key name array
Posted
by Sean McRaghty
on Stack Overflow
See other posts from Stack Overflow
or by Sean McRaghty
Published on 2010-03-25T20:42:02Z
Indexed on
2010/03/25
20:53 UTC
Read the original article
Hit count: 445
I have an array $data
fruit => apple,
seat => sofa,
etc. I want to loop through so that each key becomes type_key[0]['value'] so eg
type_fruit[0]['value'] => apple,
type_seat[0]['value'] => sofa,
and what I thought would do this, namely
foreach ($data as $key => $value)
{
# Create a new, renamed, key.
$array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;
# Destroy the old key/value pair
unset($array[$key]);
}
print_r($array);
Doesn't work. How can I make it work?
Also, I want everything to be in the keys (not the values) to be lowercase: is there an easy way of doing this too? Thanks.
© Stack Overflow or respective owner