PHP array introduction _ PHP Tutorial

Source: Internet
Author: User
PHP array introduction. PHP array introduction: In php, the subscript of an array can be an integer or a string in php. the element sequence of the array is not determined by the subscript, it is determined by the order in which the PHP array is introduced.
Array basics:

In php, the subscript of an array can be an integer or a string.

In php, the element sequence of an array is not determined by the subscript, but by the order in which it is "added ".

Definition:

$ Arr1 = array (element 1, element 2 ,......);

Array (1, 1.1, 5, 'ABC', true, false); // any data can be stored, which is the default subscript.

Array (2 => 1.1 =>, 3 => 5, 7 => 'ABC', 0 => true); // The subscript can be set at will (no order required, no consecutive)

Array (2 => 1, 1.1, 1 => 5, 'ABC', 0 => true); // you can add subscript or not, the Default subscript is used if no subscript is added.

// Default subscript rule: the maximum number of subscripts that have been used earlier + 1

// The subscript of this number is: 2, 3, 0

Array (2 => 1, 'DD' => 5, 1 => 1.1, 'ABC', 0 => true); // mixed subscript, also follows the Default subscript rules

Array (-2 => 1, 'DD' => 5, 1.1, 'ABC', true); // negative subscript is not included in the integer subscript, but only used as the character subscript

// The subscript of the last three items in the array is: 0, 1, 2

Array (2.7 => 1, 'DD' => 5, 1 => 1.1, 'ABC', true); // The floating point subscript is automatically converted to an integer, and the fractional part is directly removed.

Array ("2.7" => 1, 'DD' => 5, "11" => 1.1, 'ABC', true); // A numeric string subscript.

Array (2 => 1, 'DD' => 5, true => 1.1, 'ABC', false => true); // use a Boolean value as a subscript, true is 1, false is 0

Array (2 => 1, 'DD' => 5, 2 => 1.1, 'ABC', true); // if the subscript already exists, it simply overwrites the value of the submark with the same name.

Other forms:

$ Arr1 [] = 1;

$ Arr1 [] = 5;

$ Arr1 [] = 1.1;

... // Use [] directly after the variable to form an array and assign values in turn.

$ Arr2 ['A'] = 1;

$ Arr2 ['BB'] = 5;

$ Arr2 [5] = 1.1;

The subscript written in the form of... // is almost the same as the array syntax structure.

Array Category:

Key-value relationships are divided:

Associated array: an array that uses a subscript as a string and can generally express the meaning of data.

Example: $ person = array ("name" => "poe", "age" => 18, "edu" => "");

Index array: it usually refers to the continuous numeric subscript of an array whose subscript Strictly starts from 0-similar to that of a js array.

From the array level:

One-dimensional array: each element value in an array is a common value (non-array value)

Example: $ person = array ("name" => "poe", "age" => 18, "edu" => "");

Two-dimensional array: each item in an array is a one-dimensional array.

$ Person = array (

"Name" => array ("xiaohua", "xiaofang ),

"Age" => array (18, 22 ),

"Edu" => array ("graduated from college", "Elementary School ",)

);

Multi-dimensional array: and so on...

General syntax of multi-dimensional arrays:

$ V1 = array name [subscript] [subscript] [...]

Array traversal:

Basic traversal syntax:

Foreach ($ arr as [$ key =>] $ value ){

// All possible operations on $ key and $ value can be performed here-because they are a variable

// $ Key indicates the subscript of each element obtained, which may be a number or a string.

// $ Value indicates the value of each element, which may be of various types.

// This loop structure will traverse from the first entry of the array to the last entry, and then end

}

Array pointer and traversal principle:

Each array has a "pointer" inside it, which determines the elements obtained when the current value of the array is

In the foreach traversal process, the pointer is used.

Example: $ arr1 = array (2 => 1, 'DD' => 5, 1 => 1.1, 'ABC', 0 => true );

WKioL1ZNx1Sxflt6AAAnrLtN90Q498.jpg

In addition to the foreach loop location setting, pointers also depend on pointers:

1: $ v1 = current ($ arr1); // gets the value of the element pointed to by the current pointer in $ arr1. if it does not point to the element, it is false.

2: $ v1 = key ($ arr1); // Obtain the subscript of the element pointed to by the current pointer in $ arr1 ,.............

3: $ v1 = next ($ arr1); // move the pointer to the next element and obtain the value of the next element.

4: $ v1 = prev ($ arr1); // move the pointer to "previous element" and then obtain the value of the previous element.

5: $ v1 = reset ($ arr1); // move the pointer to "first element" and obtain the value of this element.

6: $ v1 = end ($ arr1); // move the pointer to the "last element" and obtain the value of this element.

7: $ v1 = each ($ arr1); // Obtain the subscript and value of the current element, and move the pointer to the next position.

Basics of arrays: In php, the subscript of an array can be an integer or a string in php. the element sequence of an array is determined not by the subscript, but by the order in which it is added...

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.