PHP Common Development function Parsing array Chapter [unfinished]_php tutorial

Source: Internet
Author: User
One, array processing function:
The feeling of intoxication: array processing functions are very common in PHP development, and it is important to learn good array processing functions. Array processing functions are involved in practical applications: array creation, string-to-array conversion, array-to-XML, Array to JSON. Array detection. Arrays are merged into the split. The number of arrays. Gets all the values in the array, gets all the key values in the array [subscript]
1. Create an array:
$new = Array ();
2. Implode (delimited, str) connect the array value data as specified characters
Copy CodeThe 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 cells in an array or the number of properties in an object
4.is_array (ARR) detects if a variable is an array
The 5.array_rand () function randomly selects one or more elements from the array and returns them.
Copy CodeThe code is as follows:
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r (Array_rand ($a, 1));

Output: b
Copy CodeThe code is as follows:
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r (Array_rand ($a, 2)); View Code
Array ([0] = c [1] = b)

The 6.array_sum () function returns the sum of all the values in the array.
Copy CodeThe code is as follows:
$a =array (0=> "5",1=> ",2=>" 25 ");
echo array_sum ($a);

Output: 45
The 7.array_slice () function takes a value out of the array and returns it as a condition.
Copy CodeThe code is as follows:
$a =array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
Print_r (Array_slice ($a,)); View Code
Array ([0] = Cat [1] = Horse)

The 8.array_count_values () function is used to count the occurrences of all values in an array.
Copy CodeThe 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. Array to XML
Copy CodeThe 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:
Copy CodeThe code is as follows:


Lunch
12:30

Salad
Steak


http://www.bkjia.com/PHPjc/325799.html www.bkjia.com true http://www.bkjia.com/PHPjc/325799.html techarticle one, array processing function: Drunk feeling: Array processing function is very common in PHP development, it is very important to learn the array processing function. Array processing functions are involved in the actual application of: array creation ...

  • 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.