1

I want add special character to keys of my array, like this example.
how can i convert this array

$tmp = array(
"name" => "aya",
"number" => "10");

to

$tmp = array(
"[name]" => "aya",
"[number]" => "10");  
aya
  • 1,597
  • 4
  • 29
  • 59
  • Why do you want to do that? It seems you are trying to fix another problem with that... – DrakaSAN Dec 06 '15 at 14:21
  • What is the problem with the latter code? Do a `print_r ($tmp);` and it seems to return working results. – Simo A. Dec 06 '15 at 14:32
  • I use `str_replace(array_keys($tmp), array_values($tmp))` for change `[name]` to 'aya'. but where get array from database, my array have `name` key and no `[name]`, and now i want name and number to `[name]` and `[number]` – aya Dec 06 '15 at 14:45
  • It's perhaps not elegant, but is it so hard to write a plain loop? – Karoly Horvath Dec 06 '15 at 14:47

1 Answers1

1

You can use this code:

foreach ($tmp as $key=> $value) {
    unset($tmp[$key]);
    $tmp["[$key]"] = $value;
}