What is the usage of Php array
Php array usage: An array is a collection of data, which organizes a series of data to form an operable whole. You can use the [array()] function to declare an array, and you can directly assign values to array elements.
1. What is an array
An array is a collection of data that organizes a series of data. form an operational whole. Each entity of an array contains two items: a key and a value.
2. Declaring data
There are two main ways to declare an array in PHP:
One is to declare an array using the array() function,
One is to directly assign values to array elements.
<1>array() function declares an array in the form of array([mixed...]). The syntax of the parameter mixed is key=>value
For example,
<?php $array=array("1"=>"编","2"=>"程","3"=>"词","4"=>"典"); print_r($array); echo "<br>"; echo $array[1]; //注意:下标默认是从0开始 echo $array[2]; echo $array[3]; echo $array[4]; ?>
<2>Assign values to array elements directly.
If you do not know the size of the array when you create it, or the size of the array may change when you actually write the program, it is better to use this method of array creation.
For example,
<?php $array[1]="I"; $array[2]="love"; $array[3]="PHP"; print_r($array); //输出所创建数组的结构 ?>
3. Array type
PHP supports two types of arrays: indexed array and associative array ), the former uses numbers as keys, and the latter uses strings as keys.
4. Output Array
Outputting array elements in PHP can be achieved through echo and print statements, but this can only be done on a certain element in the array. Output; to output the array structure, use the print_r() function, its syntax: print_r (mixed expression), if the parameter expression is an ordinary integer, character or real variable, the variable itself will be output. If the parameter is an array, then Displays all elements in the array in the order of a certain key value and elements.
5. Array construction
One-dimensional array:
When the elements of an array are variables, it is called a one-dimensional array.
Declare a one-bit array: Type specifier array name [constant expression];
Two-dimensional array:
When the element of an array is a one-bit array, It is called a two-dimensional array.
For example,
<?php $str = array ( "网络编程语言"=>array ("PHP","JSP","ASP"), "体育项目"=>array ("m"=>"足球","n"=>"篮球")); print_r ( $str) ; ?>
6. Traversing the array
Traversing all the elements in the array is a common operation. During the traversal process, you can Complete a query or other function. There are many ways to traverse an array in PHP. The two most commonly used methods are introduced below.
<1>Use the foreach structure to traverse the array;
<2>Use the list() function to traverse the array. The list() function can only be used for numerically indexed arrays, and the numerical index Start from 0.
Example: Comprehensive use of list() and each() to authenticate user login:
<?php //输出用户登录信息 while(list($name,$value)=each($_POST)){ if($name!="submit"){ echo "$name=$value<br>"; } } ?>
7. Count the number of array elements
In PHP , use the count() function to count the number of elements in the array, the syntax is: int coun(mixed array[,int mode]), where the parameter array is a required parameter, mode is an optional parameter, if COUNT——RECURSIVE is selected (or 1), this function will recursively pair arrays of arrays. For example,
<?php $array = array("php" => array("PHP函数参考大全","PHP程序开发范例宝典","PHP数据库系统开发完全手册"), "asp" => array("ASP经验技巧宝典") ); //声明一个二维数组 echo count($array,COUNT_RECURSIVE);//递归统计数组元素的个数,运行结果为6 ?>
8. Array sorting
<1>Use sort() and rsort() to sort the array in ascending and descending order respectively, such as,
<?php $array=array(5,26,37,18,9,42,88,66); $array1=sort($array); for($i=0;$i<count($array);$i++){ echo $array[$i]." "; } echo "<br>"; $array1=rsort($array); for($i=0;$i<count($array);$i++){ echo $array[$i]." "; } ?> 运行结果: 5 9 18 26 37 42 66 88 88 66 42 37 26 18 9 5
<2>Use ksort() and asort() to sort associative arrays
If you use associative arrays, you must keep the keys and values sorted consistently after sorting. This is necessary Use the ksort() and asort() functions
, such as,
<?php $array=array('php'=>1,'jsp'=>2,'asp'=>3); ksort($array); print_r($array); echo "<br>"; asort($array); print_r($array); ?> 运行结果: Array ( [asp] => 3 [jsp] => 2 [php] => 1 ) Array ( [php] => 1 [jsp] => 2 [asp] => 3 )
If you want to learn more about programming, please pay attention to the php training column!
The above is the detailed content of What is the usage of Php array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use PHP arrays to generate and display charts and statistical graphs. PHP is a widely used server-side scripting language with powerful data processing and graphic generation capabilities. In web development, we often need to display charts and statistical graphs of data. Through PHP arrays, we can easily implement these functions. This article will introduce how to use PHP arrays to generate and display charts and statistical graphs, and provide relevant code examples. Introducing the necessary library files and style sheets Before starting, we need to introduce some necessary library files into the PHP file

How to use PHP arrays to generate dynamic slideshows and picture displays. Slideshows and picture displays are common functions in web design and are often used in scenarios such as carousels and gallery displays. As a popular server-side scripting language, PHP has the ability to process data and generate dynamic HTML pages, and is very suitable for generating dynamic slideshows and picture displays. This article will introduce how to use PHP arrays to generate dynamic slideshows and picture displays, and give corresponding code examples. Prepare image data First, we need to prepare a set of image path data

How to use PHP arrays to implement user login and permission management functions When developing a website, user login and permission management are one of the very important functions. User login allows us to authenticate users and protect the security of the website. Permission management can control users' operating permissions on the website to ensure that users can only access the functions for which they are authorized. In this article, we will introduce how to use PHP arrays to implement user login and permission management functions. We'll use a simple example to demonstrate this process. First we need to create

PHP array averaging functions include: 1. array_sum(), which is used to calculate the sum of all values in the array. In order to calculate the average, you can add all the values in the array and then divide by the number of array elements; 2 , array_reduce(), used to iterate the array and calculate each value with an initial value; 3. array_mean(), used to return the average of the array, first calculate the sum of the array, and calculate the number of array elements, then The sum is divided by the number of array elements to get the average.

There are several ways to determine an array in PHP: 1. Use the count() function, which is suitable for all types of arrays. However, it should be noted that if the parameter passed in is not an array, the count() function will return 0; 2. Use the sizeof() function, which is more used to maintain compatibility with other programming languages; 3. Custom functions, By using a loop to traverse the array, each time it is traversed, the counter is incremented by 1, and finally the length of the array is obtained. Custom functions can be modified and expanded according to actual needs, making them more flexible.

PHP array is a very common data structure that is often used during the development process. However, as the amount of data increases, array performance can become an issue. This article will explore some performance optimization techniques for PHP arrays and provide specific code examples. 1. Use appropriate data structures In PHP, in addition to ordinary arrays, there are some other data structures, such as SplFixedArray, SplDoublyLinkedList, etc., which may perform better than ordinary arrays in certain situations.

How to convert a php array from two dimensions to a one-dimensional array: 1. Use loop traversal to traverse the two-dimensional array and add each element to the one-dimensional array; 2. Use the "array_merge" function to merge multiple arrays into An array. Pass the two-dimensional array as a parameter to the "array_merge" function to convert it into a one-dimensional array; 3. Using the "array_reduce" function, you can process all the values in the array through a callback function and finally return a result.

PHP array key-value pair is a data structure consisting of a key and a corresponding value. The key is the identifier of the array element, and the value is the data associated with the key. It allows us to store and access data using keys as identifiers. By using key-value pairs, we can more easily operate and manage elements in the array, making program development more flexible and efficient.
