Arrays for Beginners to PHP

1. An array can store multiple values in a single variable

2. What is an array

An array is a special variable that can store multiple values in a single variable

Arrays can store multiple values in a single variable, and the values can be accessed based on keys

3. How to create an array in php

array () Function is used to create arrays

4. There are 3 arrays in php

(1). Arrays with numeric ID keys----numeric arrays

(2). Array with specified keys, each key is associated with a value -------Associative array

(3). Array containing one or more arrays---- ----Multidimensional array


Numeric array


Associative array

Associative array is an array using the specified key that you assign to the array

"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>

Traverse the association Array

"35","Ben"=>"37","Joe"=>"43"); //是使用foreach 遍历关联数组 foreach($age as $x=>$x_value){ echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?>

Traverse and print all values in the associative array


Multidimensional array

'); print_r($arr); print('
'); ?>



Get the array length----------count() function

In the following case, get the length of an array

Array sorting

Array sorting, There are functions for sorting arrays in php

1.sort() - Sort the array in ascending order

2.rsort() - Sort the array in descending order

3 .asort() - Sort the array in ascending order based on the value of the associative array

4.ksort() - Sort the array in ascending order based on the keys of the associative array

5.arsort() - Sort the array in descending order according to the value of the associative array

6.krsort() - Sort the array in descending order according to the key of the associative array


sort()

"; print_r($arr); echo "
"; ?>

##rsort()

"; print_r($arr1); echo "
"; ?>


##asort()

"; print_r($arr); echo "
"; ?>


arsort()

"; print_r($arr); echo "
"; ?>

ksort()

10,'b'=>5,'c'=>20); ksort($arr); echo "
"; print_r($arr); echo "
"; ?>

krsort()

10,'b'=>5,'c'=>20); krsort($arr); echo "
"; print_r($arr); echo "
"; ?>

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!