Home  >  Article  >  Backend Development  >  Array Analysis of Common PHP Development Functions [Unfinished]_PHP Tutorial

Array Analysis of Common PHP Development Functions [Unfinished]_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:17:09959browse

1. Array processing functions:
Drunk feeling: Array processing functions are very common in PHP development, and it is crucial to learn array processing functions well. In practical applications, array processing functions involve: the creation of arrays, and the conversion of strings into arrays. Mutual conversion, array to XML, array to JSON. Array detection. Array merging and splitting. Number of arrays. Get all values ​​in the array, get all key values ​​in the array [subscript]
1. Create an array :
$new = array();
2. implode (separated, str) concatenates array value data according to specified characters

Copy code The code is as follows:

$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr); Output
Hello World! Beautiful Day!

3.count(arr) Count the number of cells in the array or the number of attributes in the object
4.is_array(arr) Check whether the variable Is an array
5.array_rand() function randomly selects one or more elements from the array and returns it.
Copy code The code is as follows:

$a=array("a"=>"Dog","b" =>"Cat","c"=>"Horse");
print_r(array_rand($a,1));

Output:b
Copy code 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.array_sum() function returns the sum of all values ​​in the array.
Copy code The code is as follows:

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

Output: 45
7.array_slice() function removes a segment from the array based on conditions value and return.
Copy code 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.array_count_values() function is used to count the number of occurrences of all values ​​in the array.
Copy code 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. Array to XML
Copy code 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:
Copy code The code is as follows:



lunch


< ;entree>salad
steak


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325799.htmlTechArticle1. Array processing functions: Drunk feeling: Array processing functions are very common in PHP development. Learn array processing functions well. It is crucial. In practical applications, array processing functions involve: the creation of arrays...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn