First PHP community-PHP array functions

Source: Internet
Author: User
Array_multisort () sorts multiple arrays or multi-dimensional arrays at a time or by one dimension. The original key-name association is retained during sorting. the implemented functions are as follows ......?

Cut a 1-dimension component into a 2-dimensional array array_chunk ()
$ Input_array = array ('A', 'B', 'C', 'D', 'E ');
Print_r (array_chunk ($ input_array, 2 ));

Compare two arrays: array_diff_assoc () or array_diff (). If the returned value is null, the two arrays are the same. Otherwise, they are different.
Use a function to filter the value array_filter () in the array ()


Function odd ($ var ){
Return ($ var % 2 = 1 );
}

Function even ($ var ){
Return ($ var % 2 = 0 );
}

$ Array1 = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5 );
$ Array2 = array (6, 7, 8, 9, 10, 11, 12 );

Echo "Odd: n ";
Print_r (array_filter ($ array1, "odd "));
Echo "Even: n ";
Print_r (array_filter ($ array2, "even "));
?>
Array_map () applies the callback function to the units of the given array. its parameters can be an array or multiple arrays, the parameters of the callback function must be the same as those of the callback function.

// Example of a single parameter, multiply each value in the array by its power 3
Function cube ($ n ){
Return $ n * $ n;
}

$ A = array (1, 2, 3, 4, 5 );
$ B = array_map ("cube", $ );
Print_r ($ B );
?>

// Example of multiple array parameters

Function show_Spanish ($ n, $ m ){
Return "The number $ n is called $ m in Spanish ";
}

Function map_Spanish ($ n, $ m ){
Return array ($ n => $ m );
}

$ A = array (1, 2, 3, 4, 5 );
$ B = array ("uno", "dos", "tres", "cuatro", "cinco ");

$ C = array_map ("show_Spanish", $ a, $ B );
Print_r ($ c );
$ D = array_map ("map_Spanish", $ a, $ B );
Print_r ($ d );
?>
// Output result
// Printout of $ c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
Array_multisort () sorts multiple arrays or multi-dimensional arrays.
It can be used to sort multiple arrays at a time or sort multi-dimensional arrays according to a certain dimension. The original key-name association is retained during sorting, and the functions implemented are SQL ......

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.