• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 100
  • Last Modified:

Rename Array Indexes

Hi  Experts,

How can I rename indexes of an associatave array in PHP Dynamically? I found the following https://stackoverflow.com/questions/9605143/how-to-rename-array-keys-in-php, but I dont understand the array_map 100%, even from php.net

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

Thank you
0
APD Toronto
Asked:
APD Toronto
1 Solution
 
gr8gonzoConsultantCommented:
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);
Array
(
    [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";
unset($myArray[3]); 

Open in new window

Now when I print_r, I'd see:
Array
(
    [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.
1
 
APD TorontoAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now