Introduction to PHP arrays

Source: Internet
Author: User

First, create an array

An array can be declared in PHP using the language structure of array ([Index=>values]). For example:

<? PHP $fruits Array (    "fruits"  array("a" and "Orange", "b" = "banana", "c" = "apple"),    array
     (1, 2, 3, 4, 5, 6),    "holes"   array("First", 5 = "Second", "third")); >

Description: The syntax "Index=>values", separated by commas, defines the index and value, which can be a string or a number. If the index is omitted, an integer index starting from 0 is automatically generated. Also, if two identical indexes are defined, the latter overwrites the previous one.

Second, common array functions

1.array_pad ()-fills a value into the array with the specified length, with the following syntax:

Array Array_pad Array $array $size $value )

description of the function:array is the original array that needs to be populated, size is the length of the new array after padding, and value is the values that will be populated. This function returns a populated array whose length is size, which is based on the original array and is populated with a number of value.

<?PHP$input=Array(12, 10, 9);$result=Array_pad($input, 5, 0);//result is an array (9, 0, 0)$result=Array_pad($input,-7,-1);//result is an array ( -1,-1,-1,-1,, 9)$result=Array_pad($input, 2, "NoOp");//Not padded?>

Description:The value of size is important if size is a positive fill that will occur on the right side of the array, if size is negative padding will occur on the left side of the array, if the absolute value of size is less than or equal to the length of the original array, no padding is made. In addition, the function simply returns a copy array, and the original array is not changed.

2.list ()-assigns the values in the array to a set of variables that can be assigned a value in a single operation, such as

<?PHP$info=Array(' coffee ', ' brown ', ' caffeine ');//List all variablesList($drink,$color,$power) =$info;Echo"$drinkIs$colorand$powerMakes it special.\n ";//list one of theirList($drink, ,$power) =$info;Echo"$drinkHas$power. \ n ";//or let's jump to the third one .List( , ,$power) =$info;Echo"I need$power!\n ";//list () cannot function on a stringList($bar) = "ABCDE";Var_dump($bar);//NULL?>

3.count ()-Calculates the number of cells in the array. Grammar:

Count $array $mode = Count_normal])

Description of function: Array is the arrays to be computed; Mode is an optional parameter, and if set to Count_recursive (or), count () counts the array recursively. is especially useful for calculating all cells of a multidimensional array.

<? PHP $food Array Array (' Orange ', ' banana ', ' apple '),              array(' carrot ', ' collard ', ' pea '); // Recursive Count Echo Count ($food//  output 8//normal countechocount($food // Output 2?>

4.range ()-Creates an array containing the specified range of cells, syntax:

Array Range $start ,$endnumber$step = 1])

description of the function:start is the first value of the sequence, end identifies the end of the sequence, and step sets the step for the generated sequence, which should be set to a positive number, which defaults to 1.

<?PHP//Step Parameters//Array (0, ten , +, +, +, +, +, +,--)foreach(Range(0, 100, 10) as $number) {    Echo $number;}//the use of the character sequence//Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ');foreach(Range(' A ', ' I ') as $letter) {    Echo $letter;}?>

5.foreach ()-This syntax structure provides a simple way to iterate through an array, with two syntaxes:

foreach  as $value )    Statement
foreach as $key $value ) Statement

The first format iterates through the given array of array_expression . In each loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward one step.

The second format does the same thing, besides, the key name of the current cell is assigned to the variable $key in each loop .

You can easily modify the elements of an array by adding & to it before $value. This method assigns a value to a reference instead of copying a value.

<? PHP $arr Array (1, 2, 3, 4); foreach ($arr as &$value) {    $value$value * 2;} // $arr is now Array (2, 4, 6, 8) unset ($value//  finally cancel the reference ?>

Note: the $value Reference of the last element of the array remains after the foreach loop and it is recommended to destroy it using unset ().

Introduction to PHP arrays

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.