Php commonly used array operations array split times key name difference set _ PHP Tutorial

Source: Internet
Author: User
Commonly used array operations in php array split times key-name difference set. This tutorial adds a large number of php array instances for beginners, including creating an array and using the first two array values as the keys and values of the new array. The data is returned as a string to split the array, this tutorial does not add to favorites a large number of php array instances for beginners, including creating an array and using the first two array values as the keys and values of the new array. The data is returned as a string to split the array, if the original array key name is not retained, the number of occurrences of the original array element is assigned to the new array, and data operations such as calculation of the key name difference set are displayed.

This tutorial adds a large number of php examples to be used by beginners, including the creation of an array using the first two array values as the keys and values of the new array, which are returned as strings to split the array, if the original array key name is not retained, the number of occurrences of the original array element is assigned to the new array, and data operations such as calculation of the key name difference set are displayed.
*/
//

$ Array = array (, =>, 3 => 13); // create an array
Print_r ($ array); // outputs the array content

//

$ A = array ('green', 'red', 'yellow'); // defines the first array.
$ B = array ('avcado', 'apple', 'bana'); // defines the second array.
$ C = array_combine ($ a, $ B); // use the first two array values as the keys and values of the new array.
Print_r ($ c); // outputs the newly created array

//
Foreach (range () as $ number) // returns array 0-12
{
Echo $ number .",";
}
Echo"

";
Foreach (range (0,100, 10) as $ number) // returns the array, 20 ...... 100
{
Echo $ number .",";
}
Echo"

";
Foreach (range ('A', 'I') as $ letter)
{
Echo $ letter .",";
}
Echo"

";
Foreach (range ('C', 'A') as $ letter) // returns the array c, B,
{
Echo $ letter .",";
}
//
$ Input_array = array ('A', 'B', 'C', 'D', 'E'); // defines the initial array
Print_r (array_chunk ($ input_array, 2); // splits the array without retaining the original array key name
Print_r (array_chunk ($ input_array, 2, true); // splits the array and retains the original array key name

//
$ Array = array (1, "hello", 1, "php", "hello"); // defines an array
Print_r (array_count_values ($ array); // assign the number of occurrences of the original array element to the new array and display it

//

$ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red "); // define array 1
$ Array2 = array ("a" => "green", "yellow", "red"); // defines array 2
$ Result = array_diff_assoc ($ array1, $ array2); // assign the difference set of the two arrays to the array
Print_r ($ result); // output the content of the difference set

//

$ Array1 = array ('blue' => 1, 'Red' => 2, 'green' => 3); // define array 1
$ Array2 = array ('green' => 4, 'blue' => 5, 'yellow' => 6); // define array 2
$ Result = var_dump (array_diff_key ($ array1, $ array2); // calculate the difference set of key names
Print_r ($ result );

//

// Define the callback function
Function key_compare_func ($ a, $ B)
{
If ($ a = $ B)
{
Return 0; // if the two parameters are equal, return 0
}
Return ($ a> $ B )? 1:-1; // if $ a> $ B returns 1, less than-1
}
// Define two arrays respectively
$ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
// Use the callback function for index check to calculate the difference set of the array
$ Result = array_diff_uassoc ($ array1, $ array2, "key_compare_func ");
Print_r ($ result );


The delimiter uses the first two array values as the keys and values of the new array and returns a string to split the array. no...

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.