Free cookie consent management tool by TermsFeed Generator PHP Array Replace | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
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

PHP Trait
Mar 22, 2023 programming

PHP Trait

Today we are going to talk about traits in PHP. A class can extend only one class. But what if you wanted to inherit multiple classes and have their functionalities in your current class? ...

4 Min Read Read More
PHP Splat Operator
Feb 15, 2023 programming

PHP Splat Operator

Today we are going to talk about splat operator in PHP. Splat operator is when we add three dots before the name of the variable and it's way more useful than you think. ...

7 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More
PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications
Jul 17, 2025 programming

PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications

PHP is often called the engine room of the modern web. Despite debates about newer technologies like Node.js, Go, or Python’s Django, PHP has remained a consistent favorite among developers. In fact, as of 2025, PHP powers over 75% of all websites that use a known server-side programming language. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...