0

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.

2
  • There is no other way, except for doing so when you create the array. Commented Apr 9, 2014 at 7:51
  • After remove referral_ your keys duplicated and overwitten. Be careful on that. And you can see my answer and working demo Commented Apr 9, 2014 at 8:04

5 Answers 5

2

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

7 Comments

This looks really nice. I had a solution that involved using foreach, but don't really want to use it, since the array might get pretty large. So yea, great!
@w0rldart: If the array is pretty large, then you might want to stick to using foreach instead. array_walk is slower when done over large arrays. Also, unlike foreach, you can't limit the actions inside the function(&$v) { ... } block, as it would get executed every time.
@ShankarDamodaran then I might not use any of these solutions at all, since they all involve some increase in the processing time, and rather use the unset version. Hadn't thought that array_walk could be slower. Thanks for the info
@w0rldart, From your question's edit , the array_walk still uses internal looping structures.
@ShankarDamodaran Yea, so it seems. Well I'll stick to unset for now, but thanks for your solution!
|
0

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]]);
}

Comments

0

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);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.