Array of common PHP development function parsing [unfinished] _ PHP Tutorial

Source: Internet
Author: User
Array of common PHP development function parsing [unfinished]. I. array processing functions: Get Drunk: Array processing functions are very common in PHP Development. learning array processing functions is crucial. array processing functions involved in practical applications: Array creation I. array processing functions:
Intoxicating: Array processing functions are very common in PHP Development. learning array processing functions is crucial. array processing functions involve the creation of arrays, the conversion of strings from arrays to XML, and the conversion of arrays to JSON. array detection. the array is merged into shards. number of arrays. obtain all values in the array and all key values in the array [subscript]
1. create an array:
$ New = array ();
2. implode (separator, str) concatenates array value data by specified characters

The code is as follows:


$ Arr = array ('hello', 'World! ', 'Beautiful', 'day! ');
Echo implode ("", $ arr); output
Hello World! Beautiful Day!


3. count (arr) calculates the number of units in the array or the number of attributes in the object.
4. is_array (arr) checks whether the variable is an array.
5. the array_rand () function randomly selects one or more elements from the array and returns them.

The code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 1 ));


Output: B

The code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 2); View Code
Array ([0] => c [1] => B)


6. the array_sum () function returns the sum of all values in the array.

The code is as follows:


$ A = array (0 => "5", 1 => "15", 2 => "25 ");
Echo array_sum ($ );


Output: 45
7. the array_slice () function extracts a value from the array based on the conditions and returns it.

The code is as follows:


$ A = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird ");
Print_r (array_slice ($ a, 1, 2); View Code
Array ([0] => Cat [1] => Horse)


8. the array_count_values () function is used to count the number of occurrences of all values in the array.

The code is as follows:


$ A = array ("Cat", "Dog", "Horse", "Dog ");
Print_r (array_count_values ($ a); output:
Array ([Cat] => 1 [Dog] => 2 [Horse] => 1)


3. convert arrays to XML

The code is as follows:


Function array2xml ($ array, $ tag ){
Function ia2xml ($ array ){
$ Xml = "";
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
$ Xml. = "<$ key>". ia2xml ($ value )." ";
} Else {
$ Xml. = "<$ key>". $ value ." ";
}
}
Return $ xml;
}
Return simplexml_load_string ("<$ tag>". ia2xml ($ array )." ");
}
$ Test ['type'] = 'lunch ';
$ Test ['Time'] = '12: 30 ';
$ Test ['menu '] = array ('entree' => 'salad', 'maincourse' => 'steak ');
Echo array2xml ($ test, "meal")-> asXML ();


Output:

The code is as follows:




Lunch
12: 30


Salad
Steak


Getting drunk: Array processing functions are very common in PHP Development. learning array processing functions is crucial. array processing functions are involved in the actual application: Creating 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.