PHP Array Replace

PHP Array Replace

Last Updated on Feb 14, 2023

Array Replace

Array replace is a very simple yet powerful function.

It takes two arguments

Array The original array

Replacements one or more arrays

PHP will compare the replacements with the original array and for every key if the value is different in the replacement array it will update the value

Let’s say I have loaded three csv files into 3 array and they look like this:

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver'
];
$peopleA = [
   'php'      => 'amir updated',
   'feedhive' => 'simon',
   'twitter'  => 'pratham updated',
   'docker'   => 'francesco',
   'web3'     => 'oliver updated',
   'saas'     => 'simon'
];
$peopleB = [
   'php'      => 'amir updated again',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco updated',
   'web3'     => 'oliver'
];

I want to update my original array people based on the values in peopleA and peopleB arrays. So let’s do it

$updatedPeople = array_replace($people,$peopleA,$peopleB);
//Array(
//    [php]       => amir updated again
//    [feedhive]  => simon
//    [twitter]   => pratham
//    [docker]    => francesco updated
//    [web3]      => oliver
//    [saas]      => simon
//)

Value in the last array will overwrite all the values in previous updates.

As you can see it has updated php based on peopleA but then updated it based on peopleB so the final value is amir updated again Or pratham was updated in the peopleA array but in peopleB array it was the same as the original so php kept that. And saas as a new key has also been added from peopleA array to the final array

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver'
];
$peopleA = [
   'php'      => 'amir updated',
   'feedhive' => 'simon',
   'twitter'  => 'pratham updated',
   'docker'   => 'francesco',
   'web3'     => 'oliver updated',
   'saas'     => 'simon'
];
$peopleB = [
   'php'      => 'amir updated again',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco updated',
   'web3'     => 'oliver'
];

$updatedPeople = array_replace($people,$peopleA,$peopleB);

print_r($updatedPeople);
//Array(
//    [php]       => amir updated again
//    [feedhive]  => simon
//    [twitter]   => pratham
//    [docker]    => francesco updated
//    [web3]      => oliver
//    [saas]      => simon
//)

If I wanted to keep the updates from peopleA over peopleB I would write that as the last argument.

$updatedPeople = array_replace($people,$peopleB,$peopleA);
//Array (
//    [php]       => amir updated
//    [feedhive]  => simon
//    [twitter]   => pratham updated
//    [docker]    => francesco
//    [web3]      => oliver updated
//    [saas]      => simon
//)

Conclusion

Now you know about array replace function in PHP.

I recommend you to open a PHP files and try to define multiple arrays. then try to replace the values inside one array with the values in other arrays.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • array replace function in PHP

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Courses