PHP array functions (2)

Source: Internet
Author: User

The 1.array_combine () function creates a new array by merging two arrays, one of which is the key name and the value of the other array is the key value

Array_combine (keys,values);

The number of elements in the key an array group and the key value array must be the same!

If one of the arrays is empty, or if the number of elements in the two array is different, the function returns FALSE.

The 2.array_count_values () function counts all the values in the array.

Array_count_values (array)

The Array_count_values () function is used to count the occurrences of all values in an array.

This function returns an array whose key name is the value of the original array, and the key value is the number of occurrences of the value in the original array.

3. The Array_diff () function returns an array of difference sets for two arrays. The array includes all of the key values in the array being compared, but not in any other parameter array.

In the returned array, the key name remains unchanged.

Array_diff (array1,array2,array3...);

Only values are used for comparisons.

4. The ARRAY_DIFF_ASSOC () function is used to compare the key names and key values of two (or more) arrays, and to return a difference set.

The function compares the key names and key values of two (or more) arrays, and returns an array of difference sets, which includes all the arrays (array1) that are being compared, but not any other array of arguments (array2 or array3 And so on) in the key names and key values.

ARRAY_DIFF_ASSOC (array1,array2,array3...);

The 5.array_diff_key () function is used to compare the key names of two (or more) arrays and return a difference set.

The function compares the key names of two (or more) arrays, and returns an array of difference sets, which includes all the arrays (array1) that are being compared, but not any other array of arguments (array2 or array3 And so on) in the key name.

Array_diff_key (array1,array2,array3...);

The Array_diff_key () function returns an array that contains all the keys in the array being compared, but not in any other parameter array.

6. The ARRAY_DIFF_UASSOC () function is used to compare the key names and key values of two (or more) arrays, and to return a difference set.

ARRAY_DIFF_UASSOC (array1,array2,array3...,myfunction);

The function uses a user-defined function to compare the key names!

The function compares the key names and key values of two (or more) arrays, and returns an array of difference sets, which includes all the arrays (array1) that are being compared, but not any other array of arguments (array2 or array3 and so on) in the key names and key values.

The ARRAY_DIFF_UASSOC () function uses a user-defined callback function (callback) to perform an index check to calculate the difference between two or more arrays. Returns an array that contains the values in array1 but not in any other parameter array.

Note that unlike the Array_diff () function, the key name is also compared.

The parameter myfunction is a user-defined function to compare two arrays, which must have two parameters-that is, two key names to be compared. Therefore, the behavior of the function Array_diff_assoc () is reversed, and the latter is compared by an intrinsic function.

The key names in the returned array remain unchanged.

function MyFunction ($a, $b) {if ($a = = = $b)  {  return 0;  }  return ($a > $b) 1:-1;} $a 1=array ("a" and "Red", "b" = "green", "c" = "blue"), $a 2=array ("a" and "Red", "B" and "green", "D" and "Blue") ); $a 3=array ("e" = "Yellow", "a" and "red", "D" and "Blue"); $result =array_diff_uassoc ($a 1, $a 2, $a 3, "MyFunction") ;p Rint_r ($result);

7. The Array_diff_ukey () function is used to compare the key names of two (or more) arrays and return a difference set.

Array_diff_ukey (array1,array2,array3...,myfunction);

The function compares the key name with a user-defined function!

The function compares the key names of two (or more) arrays, and returns an array of difference sets, which includes all the arrays (array1) that are being compared, but not any other array of arguments (array2 or array3 And so on) in the key name.

Array_diff_ukey () returns an array that contains all the values that appear in the array1 but do not appear in any of the other parameter array's key names. Note that the association relationship remains unchanged. Unlike Array_diff (), comparisons are made by key names rather than values.

This comparison is done through a user-supplied callback function. If you think that the first parameter is less than, equal to, or greater than the second argument, you must return an integer less than 0, equal to zero, or greater than 0, respectively.

8. The Array_fill () function fills the array with key values.

Array_fill (index,number,value);

The Array_fill () function populates the array with the given values, the returned array has number elements, and the value is values. The returned array uses a numeric index, starting from the start position and incrementing. If number is 0 or less than 0, an error occurs.

9. The Array_fill_keys () function fills the array with the specified key and value.

Array_fill_keys (keys,value);
$keys =array ("A", "B", "C", "D"), $a 1=array_fill_keys ($keys, "Blue");p Rint_r ($a 1);

PHP array functions (2)

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.