Alex
Alex

Reputation: 7688

Changing key's name in a array

So, having an array like the following:

$input = [
    'first_name'   => 'Alex',
    'last_name'    => 'UFO',
    'email'        => '[email protected]',
    'phone_number' => '124124',
    // .....
    'referral_first_name'   => 'Jason',
    'referral_last_name'    => 'McNugget',
    'referral_email'        => '[email protected]',
    'referral_phone_number' => '1212415',
];

After processing the first part, until referral..., do you think of any better way to replace referral_first_name with first_name and so on, then the following? Maybe a more dynamic and automatic way.

$input['first_name'] = $input['referral_first_name'];
unset($input['referral_first_name']);

$input['last_name'] = $input['referral_last_name'];
unset($input['referral_last_name']);

$input['email'] = $input['referral_email'];
unset($input['referral_email']);

$input['phone_number'] = $input['referral_phone_number'];
unset($input['referral_phone_number']);

Guys, I forgot to mention, but I have already done it with foreach, but the problem will be when the array gets pretty large (and usually does, and not by one person using that function, but by many), and that would mean extra unnecessary processing time, since it has to iterate through the whole array, before reaching the referral_.. part.

Upvotes: 0

Views: 58

Answers (5)

Recreating the array is the only way..

  • Grab all the keys from the original array and put it in a temp array
  • Use array_walk to modify that temp array to add the referral word to it
  • Now use array_combine using the above keys and values from the old array.

The code..

<?php
$input = [
    'first_name'   => 'Alex',
    'last_name'    => 'UFO',
    'email'        => '[email protected]',
    'phone_number' => '124124'];

$ak = array_keys($input);
array_walk($ak,function (&$v){ $v = 'referral_'.$v;});
$input=array_combine($ak,array_values($input));
print_r($input);

OUTPUT:

Array
(
    [referral_first_name] => Alex
    [referral_last_name] => UFO
    [referral_email] => [email protected]
    [referral_phone_number] => 124124
)

Since you are looking for performance , Use a typical foreach

$newarr = array();
foreach($input as $k=>$v ) {
$newarr["referral_".$k]=$v;
}

Upvotes: 1

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

You can use following;

function changeKeys($input) {
    $keys = array_keys($input);
    foreach($keys as $key) {
        if (strpos($key, "referral_") !== false) {
            $tempKey = explode("referral_", $key);
            $input[$tempKey[1]] = $input[$key];
                unset($input[$key]);
        }
    }   
    return $input;
}

changeKeys($input);

Here is a working demo: codepad

Note: Be sure that, your keys overwrited due to duplicate keys after "referral_" removal

Upvotes: 0

ponciste
ponciste

Reputation: 2229

you must create another array, this code should do it dynamically:

$newInput = array();
foreach($input as $key => $element){
    $newKey = explode("_", $key, 2);
    $newInput[$newKey[1]] = $element;
}

OUTPUT

enter image description here

hope this helps :-)

Upvotes: 2

solvease
solvease

Reputation: 539

try the below one...

$input = [
   //'first_name'   => 'Alex',
   // 'last_name'    => 'UFO',
   // 'email'        => '[email protected]',
    //'phone_number' => '124124',
    // .....
    'referral_first_name'   => 'Jason',
    'referral_last_name'    => 'McNugget',
    'referral_email'        => '[email protected]',
    'referral_phone_number' => '1212415',
];

foreach ($input as $key=>$value){
    $refereal_pos = strpos($key, 'referral_');
    if($refereal_pos !== FALSE && $refereal_pos == 0){
        $input[str_replace('referral_', '', $key)] = $value;
        unset($input[$key]);
    }
}

print_r($input);

Upvotes: 0

Wietse
Wietse

Reputation: 370

How about something like this:

$replace = array('first_name', 'last_name', 'email');
foreach($replace AS $key) {
    $input[$key] = $input['referral_' . $key];
    unset($input[$input['referral_' .$key]]);
}

Upvotes: 0

Related Questions