PHP uses a custom method to implement an array merge sample _php tips

Source: Internet
Author: User
Tags arrays php programming php regular expression

The examples in this article describe how PHP implements array merging using a custom method. Share to everyone for your reference, specific as follows:

With powerful array capabilities in PHP, there are two ways to merge arrays: Array_merge and Array_merge_recursive

But for our ever-changing business, these built-in methods don't exactly meet our requirements.

As I have now encountered the problem of array merging, the requirement is this:

There are two two-dimensional arrays, most of which have the same health name, but some health names may not exist in another array, and what I need to do now is to copy all the health values from the second array into the first array (provided that the values of one of the two arrays are the same)

It is clear that the built-in two methods do not meet the requirements (two methods simply copy the health values from the second array to the first corresponding health)

$ar 1 = Array ("COLOR" => Array ("Favorite" => "Red", "name" => "Test"));
$ar 2 = Array ("COLOR" => Array ("Favorite" => "green", "code" => "Code"));
Print_r (Array_merge ($ar 1, $ar 2));
Print_r (Array_merge_recursive ($ar 1, $ar 2));

You can see that array_merge just copies the value of each second array to the first array, apparently the array is favorite.name by the array two code, and Array_merge_ Recursive will not overwrite the same health name but once the two array health names are the same, it creates a new array and adds the values in the two array to the new array

Here's how I customize the implementation:

/** * @Author: HTL * @Description: Copy all health values in the specified health name $ar2 to the $AR1 corresponding health value * @Return: $ar 1/function Array_more_merge ($ar
    1, $ar 2, $key _name) {if (! $ar 1 | |!is_array ($ar 1) | | count ($ar 1) <=0 | |! $ar 2 | |!is_array ($ar 2) || Count ($ar 2) <=0 | | ! $key _name | |
  strlen ($key _name) <=0) {return $ar 1;  }/* Cannot use for To loop, because if the index in the array is not contiguous, use for may not properly complete the loop such as $a = Array (1 => array ("id" => 1), 8 =>
  Array ("id" => 2), 4 => Array ("id" => 4)); The array number is 3 so only [1] can be found, and [8][4] is not processed in the loop because $i <=count ($a) is not established <br>/for ($i =0; $i <=count ($a); $i + +) {Prin
  T_r ($a [$i]); foreach ($ar 1 as $i => $items) {foreach ($ar 2 as $key => $item) {if ($ar 2[$key] [$key _name]== $ar 1[$i] [$key
        _name]) {$keys = Array_keys ($ar 2[$key]);
        foreach ($keys as $name) {$ar 1[$i] [$name] = $ar 2[$key] [$name];
return $ar 1; } $ar 1 = ' {1 ': {"cost": "1", "id": "1", "tOtal_price ":" 5 "}," 2 ": {" cost ":" 2 "," id ":" 2 "," Total_price ":" Ten "}," 4 ": {" cost ":" 4 "," id ":" 4 "," Total_price ":" 20 "}} ';
$ar 1 = json_decode ($ar 1,true); $ar 2 = ' {1 ': {' cost ': ' 1 "," id ":" 1 "," Total_price ":" 5 "}," 2 ": {" cost ":" The "," id ":" 2 "," Total_price ":" "}," 4 ": {" cost ":
"", "id": "4", "Total_price": "200"};
$ar 2 = Json_decode ($ar 2,true);
echo "Array_merge_recursive:";
Print_r (Array_merge_recursive ($ar 1, $ar 2));
echo "Array_merge:";
Print_r (Array_merge ($ar 1, $ar 2));
echo "Array_more_merge:";
Print_r ($this->array_more_merge ($ar 1, $ar 2, "id"));

 Die ();

The results of the operation are shown in the following illustration:

More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.