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

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP xml and json
Mar 21, 2023 programming

PHP xml and json

Today we are going to talk about working XML and JSON in PHP. XML and JSON are the most common formats that are used in APIs. knowing how to work with them is essential for any web developer. ...

9 Min Read Read More
PHP Interface
Mar 22, 2023 programming

PHP Interface

Today we are going to talk about interfaces in PHP. Interface is like a skeleton for the class. Interface is an important topic in OOP and it helps you manage your code better. ...

6 Min Read Read More
Top Skills for Developers: Non-Technical and Technical Essentials
Apr 05, 2023 programming

Top Skills for Developers: Non-Technical and Technical Essentials

Being a successful developer requires a combination of both technical and non-technical skills. ...

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