Introduction to PHP Commonly Used Array Internal Functions (Array Functions)_PHP Tutorial

WBOY
Release: 2016-07-21 15:09:38
Original
877 people have browsed it

This chapter describes several commonly used PHP array internal functions.
We have introduced PHP arrays before. To create an array, use the array() function, and to delete an array element, use the unset() function. In this chapter we will also learn some other commonly used internal functions related to arrays.
count,sizeof
count - Returns the number of elements in an array. sizeof is an alias of count. Its function is the same as count, and it also returns the number of elements in an array.
An example of the count function is as follows. In the following example, the number of elements in the output array is 6.

Copy code The code is as follows:

$a = array(1,2,4, 5,3,9);
echo count($a); //6
?>

sort
sort - gives an array The elements are sorted. After sorting, the original keys of each element of the array are also changed due to sorting. An example of the sort function is as follows:
Copy code The code is as follows:



< ?php
$a = array(1,2,4,5,3,9);
echo "before sorting:
";
foreach ($a as $key= >$value)
{
echo "a[$key]: $value
";
}
sort($a);
echo "after sorting :
";
foreach ($a as $key=>$value)
{
echo "a[$key]: $value
";
}
?>



The returned display result is:
Copy code The code is as follows:

before sorting:
a[0]: 1
a[1]: 2
a[2 ]: 4
a[3]: 5
a[4]: 3
a[5]: 9
after sorting:
a[0]: 1
a[ 1]: 2
a[2]: 3
a[3]: 4
a[4]: 5
a[5]: 9

asort
asort - Sort the elements of the array, retaining the original key of each element.
We change sort($a) in the above example to asort($a), and the result is:
Copy code The code is as follows :

before sorting:
a[0]: 1
a[1]: 2
a[2]: 4
a[3]: 5
a[4]: 3
a[5]: 9
after sorting:
a[0]: 1
a[1]: 2
a[4]: 3
a[2]: 4
a[3]: 5
a[5]: 9

ksort
ksort - based on key The size of each element of the array is sorted. An example of the ksort function is as follows:
Copy code The code is as follows:



$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>" apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key : $val
";
}
?>



The returned results are as follows:
Copy the code The code is as follows:

a : orange
b : banana
c : apple
d : lemon

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327294.htmlTechArticleThis chapter describes several commonly used PHP array internal functions. We have introduced PHP arrays before. To create an array, use the array() function, and to delete an array element, use the unset() function. Ben...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!