An Introduction to Arrays _php Tutorial

Source: Internet
Author: User
Tags http cookie php programming
Array

1. Overview

Arrays provide a quick and easy way to manage a set of related data, which is an important part of PHP programming. Through the array can store, sort, insert and delete a large number of the same data, so as to effectively improve the program development efficiency and improve the way the program is written.

Array is a set of data, the data in accordance with a certain rules organized to form an operational whole, is a large number of data to effectively organize and manage one of the means, through the array function can be a large number of properties of the same data storage, sorting, insertion and deletion operations, Thus, it can effectively improve the efficiency of program development and improve the way of writing programs.

2. Array type

The essence of an array is to store, manage, and manipulate a set of variables. In PHP, the array is divided into one-dimensional arrays, two-dimensional arrays and multidimensional arrays, but whether it is one-dimensional or multidimensional, the array can be uniformly divided into numeric index array and associative array of two, the numeric index array uses the number as the key name, the associative array uses the string as the key name.


Numeric index array, subscript (key name) consists of a number, the default starting from 0, each number corresponds to the position of the array element in the array, do not need to specify, PHP automatically assigns the numeric index array key name an integer value, and then from this value to start the automatic increment, you can also specify where to start.

An array of associative arrays, in which the subscript (key name) consists of a mixture of numeric values and strings.

The array needs to use the key name to access the values stored in the array.


3. Declaring an array
Note: 1. The array name starts with $, the first character is a letter or an underscore, followed by any number of letters, numbers, or underscores.
2. In the same program, scalar variables and array variables cannot have duplicate names.
3. The names of the arrays are case-sensitive

There are two ways to declare an array, namely, user Declaration and function declaration.

1. User Declaration
$array [' 0 ']= "php array";
$array [' 1 ']= "PHP learning";

2. Function declaration
$arrr =array (' one ' = ' php ', ' both ' = ' java ');

Create a two-dimensional array, which is created with only one column of data content, so called a one-dimensional array, if you combine two one-dimensional array arrays into an array then it is a two-dimensional array.
Example: $str =array (
"Network" =>array ("IP Address", "Gateway");
"Book" =>array ("Spring and Autumn", "Warring states");
)


4, iterating through the array
Iterating through an array is the order in which each element in the array is accessed sequentially, until the end of the visit. In PHP, you can iterate through the array through the process statements (foreach and for Loop statements) and Functions (list () and each ()).

foreach (),
For to iterate through the array, use the count () function to get the quantity. If the array to be traversed is a numeric index array, and the index value of the array is a sequential integer, you can use the For loop to traverse, but the precondition is that you need to apply the count () function to get the number of elements in the array and then take the number of elements obtained as the execution condition for the For loop to complete the loop of the array
Example: for ($i =0; $i
"}

Through array functions list and each to iterate through the array:

The list () function assigns the values in the array to variables that can only be used for arrays of numeric indexes, and the numeric index starts at 0, and the syntax: void list (mixed ...) parameter mixed is the variable name assigned
The each () function returns the key names in the array and the corresponding values, and moves the array's pointer forward, syntax: array each (array array) parameter to the input arrays.
Example: Use the list function to get the values of the returned array in each function, assign to $name and $value, and then use the while loop output
while (list ($name, $value) =each ($array)) {echo $name = $value}
5. Output array elements
The Print_r () function can output the structure of an array, or you can use the Var_dump () function to output an array structure.
The Echo statement simply outputs an element in the array, with an identifier and array index mates, and the format echo $array [0], and the same print statement can output an element in the array.

6.php array functions
More commonly used array functions in development.

The count () function can count the number of elements in an array
The Array_push () function is to add elements to the array. Adds the passed-in element to the end of an array and returns the total number of new cells in the array. Syntax: Array_push ($array, "new element 1", "new element 2")
The Array_pop function can get and return the last element in the array and subtract the length of the array by 1 if the array is empty (or not an array) and returns null

To delete a repeating element in an array:
The Array_unique () function can remove duplicate elements from an array
Use unset () to delete an element in an array. Unset ($arr _int[1]);

Gets the key name of the specified element in the array: Gets the key name of the specified element in the array is mainly implemented by an array function.

Array_search () is used to get the key name of the specified element in the array, search for the given value in the array, return the key name after finding it, or return false, the case-sensitive letter
Array_search (Orange, $arr [case sensitive])

Use the Array_keys () function to get all the key names of the repeating elements in the array

The conversion of strings to arrays is essential, primarily using the explode and implode functions.
Explode
Implode converting an array element to a string type

7.php Global Array

Using the PHP global array, you can get a lot of environment-related information, such as current user session, user operating environment, local operating environment, etc.

$_server[] Global Array to get information about server and client configuration and current requests
$_get[] and $post[] global arrays, respectively, to receive the data passed to the current page by the GET and POST methods.
$_cookie[] Global Array stores information passed to the script via an HTTP COOKIE
$_env[] To provide information about the server
$_request[] can use it to get information about Get,post and HTTP cookies passed to the script.
$_session[] To get information about a session variable
$_files[] Special array, which is explained in detail later


Review:

1. Array overview
2.2 Types of array type
3. Declaring array user creation and function creation, one-dimensional arrays, two-dimensional arrays
4. Traverse, output array, foreach and for and Function list (), and each (), to iterate through the multidimensional array. Output with Var_dump,print_r,echo $ss [1]
The 5.php array function counts the array elements, adds elements to the array, gets the last element in the array, deletes the repeating elements in the array, gets the key names of the specified elements in the array
6.php global arrays which ones are common

The author "technology is king"

http://www.bkjia.com/PHPjc/478634.html www.bkjia.com true http://www.bkjia.com/PHPjc/478634.html techarticle Array 1. An overview array provides a quick and easy way to manage a set of related data, which is an important part of PHP programming. Arrays can be used for a large number of data of the same nature ...

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