Definition and assignment of arrays in the PHP getting started tutorial

Source: Internet
Author: User
Tags array definition

There are several methods for defining arrays in php. Next we will introduce several common methods for defining and assigning arrays in php. I hope this article will be helpful to beginners.

First, let's take a look at the array. An array is to put a group of data together in order. PHP arrays are a little different from other language Arrays: first, the stored data can be of any type; second, the array index can be numbers or strings.


You can use one of the following methods to create an array in PHP:

The Code is as follows: Copy code

<? Php
$ A = "abcd ";
Print ($ a [0]. "". $ a [1]. "". $ a [2]. "". $ a [3]. "");

?>

Result: a B c d

Method 2:

The Code is as follows: Copy code

<? Php
$ Http = array ("www", "helpphp", "cn ");
Print ($ http [0]. ".". $ http [1]. ".". $ http [2]);

?>

PHP array. To put it bluntly, each array of associated data is saved in the form of [index, value. The index starts with 0 by default. If no index is specified, PHP automatically generates an index from 0. If an index is specified, PHP starts with the next integer of the maximum positive integer of the index. If you specify decimal places, PHP will take the integer part as the index.

In addition, let's talk about other small items in the array:
Array () can declare an empty array;
Array [] = $ value: When an array exists, an array is generated and data is appended when the array is not saved.
Array [$ index] = $ value when an array exists, an array is generated and data is appended when the array does not exist.

See the following code:

The Code is as follows: Copy code

// Declare an array
$ Test01 = array ();
// Append data
$ Test01 [] = "a"; // array (0 => "");
// Append the data whose index is "a" and whose data is "B"
$ Test01 ["a"] = "B"; // array (0 => "a", "a" => "B ");
// Modify the data whose index is 0
$ Test01 [0] = "c"; // array (0 => "c", "a" => "B ");
// Another declaration method
$ Test02 = array ("a", "B", "c"); // array (0 => "a", 1 => "B ", 2 => "c ");
// Although the data of a string index is declared, the default index starts from 0.
$ Test03 = array ("a" => "a", "B", "c"); // array ("a" => "", 0 => "B", 1 => "c ");
// The maximum index in the Declaration is 2. Although the index is 0 recently, the default index starts from 3.
$ Test04 = array (2 => "a", 0 => "B", "c"); // array (2 => "", 0 => "B", 3 => "c ");
// Declaring a decimal index takes the integer part of the index. If an index is specified, the previously declared value is modified.
$ Test05 = array ("a", 2.7 => "B", 0 => "c"); // array (0 => "c ", 2 => "B ");
// Although a negative index is declared, the default index starts from 0.
$ Test06 = arra
Y (-2 => "a", "B", "c"); // array (-2 => "a", 1 => "B ", 2 => "c ");
// Multi-dimensional array Definition
$ Test07 = array ($ test01, $ test02, $ test03 );


Then we will introduce some filling functions of the array. Most of these functions can be found in the manual, so we will just give a brief introduction.

Range ($ n, $ m); specifies the range of values. For example, range (2, 4) generates an array (2, 3, 4 ).
Count ($ array); returns the size of the array.
Array_pad ($ array, $ length, $ value); returns an array with a length of $ length. The complement value of the original array is $ value, and the length is sufficient to return the original array.

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.