PHP uses a custom method to implement array merging example, array example

Source: Internet
Author: User
Tags array example

PHP uses a custom method to implement array merging example, array example

This example describes how PHP uses a custom method to merge arrays. We will share this with you for your reference. The details are as follows:

PHP provides powerful array functions and provides two methods for Array merging: array_merge and array_merge_recursive.

However, for our ever-changing business, these built-in methods do not fully meet our requirements.

For example, I have encountered the problem of array merging. The requirement is as follows:

There are two-dimensional arrays. Most of these two arrays have the same health name, but some health names may not exist in another array, what I need to do now is copy all the values in the second array to the first array (condition: when the values of one of the two arrays are the same)

Obviously neither of the built-in Methods meets the requirements (both methods only copy the values in the second array to the first corresponding Jian)

$ar1 = array("color"=> array("favorite"=>"red","name"=>"test"));$ar2 = array("color"=> array("favorite"=>"green","code"=>"code"));print_r (array_merge ($ar1, $ar2));print_r(array_merge_recursive ($ar1, $ar2));

We can see that array_merge only copies the value of each second array to the first array. Obviously, array 1 is favorite. the name is overwritten by two codes of the array, while array_merge_recursive does not overwrite the same health name. But once the two arrays have the same health name, it creates a new array and adds the values of the two arrays to the new array.

The following is a custom implementation method:

/*** @ Author: HTL * @ Description: Copy all the values of $ ar2 to the corresponding values of $ ar1. * @ Return: $ ar1 */function array_more_merge ($ ar1, $ ar2, $ key_name) {if (! $ Ar1 |! Is_array ($ ar1) | count ($ ar1) <= 0 |! $ Ar2 |! Is_array ($ ar2) | count ($ ar2) <= 0 |! $ Key_name | strlen ($ key_name) <= 0) {return $ ar1;}/* you cannot use for loops, because if the index in the Array is not continuous, you may not be able to use for the correct loop, for example, $ a = Array (1 => Array ("id" => 1 ), 8 => Array ("id" => 2), 4 => Array ("id" => 4); the number of arrays is 3, so only [1] can be found. [8] [4] won't be processed in the loop, because $ I <= count ($ a) is not true <br> */for ($ I = 0; $ I <= count ($ a); $ I ++) {print_r ($ a [$ I]);} foreach ($ ar1 as $ I => $ items) {foreach ($ ar2 as $ key => $ item) {if ($ ar2 [$ key] [$ key_name] = $ ar1 [$ I] [$ key_name]) {$ keys = array_keys ($ ar2 [$ key]); foreach ($ keys as $ name) {$ ar1 [$ I] [$ name] = $ ar2 [$ key] [$ name] ;}}} return $ ar1 ;} $ ar1 = '{"1": {"cost": "1", "id": "1", "total_price": "5"}, "2 ": {"cost": "2", "id": "2", "total_price": "10"}, "4": {"cost": "4 ", "id": "4", "total_price": "20"} '; $ ar1 = json_decode ($ ar1, true); $ ar2 =' {"1 ": {"cost": "1", "id": "1", "total_price": "5"}, "2": {"cost": "20 ", "id": "2", "total_price": "100"}, "4": {"cost": "40", "id": "4 ", "total_price": "200"} '; $ ar2 = json_decode ($ ar2, true); echo "array_merge_recursive:"; print_r (array_merge_recursive ($ ar1, $ ar2 )); echo "array_merge:"; print_r (array_merge ($ ar1, $ ar2); echo "array_more_merge:"; print_r ($ this-> array_more_merge ($ ar1, $ ar2, "id"); die ();

Shows the running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.