On the merging of PHP arrays

Source: Internet
Author: User

Note: Try not to manipulate the database in the loop.

1. Two one-dimensional arrays merged into one one-dimensional array

$a = Array (' Morning ', ' afternoon ', ' Night ');

$b = Array (' Breakfast ', ' lunch ', ' dinner ');

(1) The array element in $ A is the key name, and the array element in $b is a new array formed by the key value.

$c = Array_combine ($a, $b);

(2) The array element with the same key value as the $b in $ A is removed, preserving only the different array elements in $ A. That is, returns the difference set.

$c =array_diff ($a, $b);

(3) Remove the array elements in $ A with the same key name and key value as the $b, preserving only the different array elements in $ A, that is, returning the array of difference sets.

$a = Array (' Morning ', ' afternoon ', ' Night ');
$b = Array (' Breakfast ', ' Lunch ', ' Night ');
$c =array_diff_assoc ($a, $b);
Print_r ($c);

The output is:

(4) Remove the array element with the same key name as the $b in $ A, preserving only the different array elements in $ A, which means returning the array of difference sets.

$c =array_diff_key ($a, $b);

An empty array.

(5) The array elements in $ A that are identical to the key and key values in the $b are removed, preserving only the different array elements in $ A, that is, returning the difference array, and then using the user-defined function to compare the key names in the array of differences and reorder the groups.

(6) Remove the array element with the same key name as the $b in $ A, preserving only the different array elements in $ A, that is, returning the difference array, and then using the user-defined function to compare the key names in the array of differences and reorder the groups.

$a = Array (' Morning ', ' afternoon ', ' Night ', ' Today ', ' Yesterday ', ' tomorrow ');
$b = Array (' Breakfast ', ' Lunch ', ' Night ');
$c =array_diff_ukey ($a, $b, function ($a, $b) {
if ($a = = $b)
return 0;
return ($a > $b) -1:1;
});
Print_r ($c);

On the merging of PHP arrays

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.