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

Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

7 Min Read Read More
How to Split an Excel File into Multiple Files Using PHP
May 07, 2024 programming

How to Split an Excel File into Multiple Files Using PHP

Sometimes you might have an excel files that you need to split. splitting the file into multiple files with PHP can be easily done. ...

9 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

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