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 Variables
Mar 21, 2023 programming

PHP Variables

Today we are going to learn about variables in PHP. Variables are very important and powerful in any programming language. So if you are learning PHP it's very important to know about variables. ...

5 Min Read Read More
PHP execution time
Feb 15, 2023 programming

PHP execution time

Today we are going to talk about calculating execution time in PHP. Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole. ...

3 Min Read Read More
PHP Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More
Top Open Source Licenses: Understanding Your Options
May 07, 2024 programming

Top Open Source Licenses: Understanding Your Options

Open source licenses dictate how software can be used, modified, and distributed. Understanding the different types of licenses is essential for developer. ...

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 ...