Rename Array Indexes

Hi  Experts,

How can I rename indexes of an associatave array in PHP Dynamically? I found the following, but I dont understand the array_map 100%, even from

Also, what if my array has 20 indexes and I only need to rename 2?

Thank you
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NOTE: Just for the sake of preserving content in case the original link goes away, APD_Toronto is referring to this function:
$tags = array_map(function($tag) {
    return array(
        'name' => $tag['name'],
        'value' => $tag['url']
}, $tags);

Open in new window

...which comes from users "kamal pal" and "alex" on SO.

Okay, to answer your first question, you can't rename indexes. For example, if I had an array like this:
// print_r($myArray);
    [0] => "Foo"
    [1] => "Bar"
    [2] => "Hello"
    [3] => "World"

Open in new window

...and I wanted to change a numeric index from "3" to the word "Three", I'd have to add a new entry and remove the old one:
$myArray["Three"] = "World";

Open in new window

Now when I print_r, I'd see:
    [0] => "Foo"
    [1] => "Bar"
    [2] => "Hello"
    [Three] => "World"

Open in new window

The array_map() function isn't magic - it's simply a shortcut that lets you run a function on every single entry in an array. So for example, let's say I wanted to add an exclamation mark to the end of every entry in $myArray. I could do this:
function addExclamationMarks($arrayEntry)
  return $arrayEntry . "!";
$tags = array_map("addExclamationMarks", $tags);

Open in new window

This would be the exact same thing as doing this:
$myArray[0] = addExclamationMarks($myArray[0]);
$myArray[1] = addExclamationMarks($myArray[1]);
$myArray[2] = addExclamationMarks($myArray[2]);
$myArray["Three"] = addExclamationMarks($myArray["Three"]);

Open in new window

So the original array_map code snippet you were looking at basically loops through every child array in the parent array and REPLACES the child array with a copy that uses a different index name (takes whatever was in the old "url" field and puts it into a "value" field instead).

The author used some code shortcuts - an anonymous function - to make the code as small as possible, but that's about it.

So if you just need to rename the index on a couple of entries in your array, it's probably better NOT to use array_map() since it will try to run on EVERY element in your array instead of just the couple that you care about. Even if the callback function has logic in it to skip over the undesired elements, it's still running that function 20 times if you have 20 entries in your array.

With only a couple of entries to be renamed, just do the manual replace (like I did with changing "3" to "Three" above) for the most efficiency.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.