Sara Fuerst
Sara Fuerst

Reputation: 6068

Changing all Keys of an Array

I have an array called $row:

$row = array(
    "comments" => "this is a test comment", 
    "current_path" => "testpath"
)

I have another array called $dictionary:

$dictionary= array(
    "comments" => "comments", 
    "current_directory" => "current_path"
)

I want to change the keys in $row to the key associated with the matching value in $dictionary.

For example, in the case above, $row would become:

$row = array(
    "comments" => "this is a test comment", 
    "current_directory" => "testpath"
)

I have tried using array_map but this doesn't seem to be changing anything:

array_map(function($key) use ($dictionary){
  return array_search($key, $dictionary);
}, array_keys($row)); 

How would I change the key properly?

Note from comment:

Unfortunately, there will generally be more entries in $dictionary then $row and the order cannot be guaranteed

Upvotes: 1

Views: 80

Answers (3)

mickmackusa
mickmackusa

Reputation: 47904

There are a couple potential "gotcha"s in the solution for your case. Since your two arrays might not have equal size, you will have to use array_search() inside a loop. Also, though it seems unlikely for your case, I would like to mention that if $dictionary has the keys: "0" or 0 then array_search()'s return value must be strictly checked for false. Here is the method that I recommend:

Input:

$row=array(
    "comments"=>"this is a test comment", 
    "title"=>"title text",                      // key in $row not a value in $dictionary
    "current_path"=>"testpath"
);

$dictionary=array(
    "0"=>"title",                               // $dictionary key equals zero (falsey)
    "current_directory"=>"current_path",
    "comments"=>"comments", 
    "bogus2"=>"bogus2"                          // $dictionary value not a key in $row
);

Method (Demo):

foreach($row as $k=>$v){
    if(($newkey=array_search($k,$dictionary))!==false){  // if $newkey is not false
        $result[$newkey]=$v;                    // swap in new key
    }else{
        $result[$k]=$v;                         // no key swap, store unchanged element
    }
}
var_export($result);

Output:

array (
  'comments' => 'this is a test comment',
  0 => 'title text',
  'current_directory' => 'testpath',
)

Upvotes: 1

Dana Case
Dana Case

Reputation: 84

I would just do a manual loop and output to a new variable. You cant use array_map or array_walk to change the structure of an array.

<?php
   $row = ["comments" =>"test1", "current_path" => "testpath"];

   $dict = ["comments" => "comments", "current_directory" => "current_path"];

    foreach($row as $key => $value){
       $row2[array_search($key, $dict)] = $value;
   };

  var_dump($row2);

 ?>

Upvotes: 0

tereško
tereško

Reputation: 58444

If $dictionary can be flipped, then

$dictionary = array_flip($dictionary);

$result = array_combine(
    array_map(function($key) use ($dictionary){ 
        return $dictionary[$key]; 
    }, array_keys($row)),
    $row
);

If not, then you will be better off doing a manual loop.

Upvotes: 1

Related Questions