Definition and assignment of arrays in the PHP Getting Started Tutorial

Source: Internet
Author: User
Tags array definition
First, let's take a look at Arrays. arrays 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,

First, let's take a look at Arrays. arrays 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 index of the array can be a number or a string.

To create an array in PHP, you can use one of the following methods to create an array. the instance code is as follows:

  1. $ A = "abcd ";
  2. Print ($ a [0]. "". $ a [1]. "". $ a [2]. "". $ a [3]. "");
  3. ?>
  4. // Result: a B c d

Method 2: The code is as follows:

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

PHP array. to put it bluntly, each array of associated data is saved in the form of [index, value]. The index defaults to 0. when no index is specified, PHP automatically generates an index from 0. when an index is specified, PHP starts from the next integer of the maximum positive integer of the index. if you specify a decimal number, 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:

  1. // Declare an array
  2. $ Test01 = array ();
  3. // Append data
  4. $ Test01 [] = "a"; // array (0 => "");
  5. // Append the data whose index is "a" and whose data is "B"
  6. $ Test01 ["a"] = "B"; // array (0 => "a", "a" => "B ");
  7. // Modify the data whose index is 0
  8. $ Test01 [0] = "c"; // array (0 => "c", "a" => "B ");
  9. // Another declaration method
  10. $ Test02 = array ("a", "B", "c"); // array (0 => "a", 1 => "B ", 2 => "c ");
  11. // Although the data of a string index is declared, the default index starts from 0.
  12. $ Test03 = array ("a" => "a", "B", "c"); // array ("a" => "", 0 => "B", 1 => "c ");
  13. // The maximum index in the declaration is 2. although the index is 0 recently, the default index starts from 3.
  14. $ Test04 = array (2 => "a", 0 => "B", "c"); // array (2 => "", 0 => "B", 3 => "c ");
  15. // Declaring a decimal index takes the integer part of the index. if an index is specified, the previously declared value is modified.
  16. $ Test05 = array ("a", 2.7 => "B", 0 => "c"); // array (0 => "c ", 2 => "B ");
  17. // Although a negative index is declared, the default index starts from 0.
  18. $ Test06 = arra
  19. Y (-2 => "a", "B", "c"); // array (-2 => "a", 1 => "B ", 2 => "c ");
  20. // Multi-dimensional array definition
  21. $ 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, such as range (2, 4) to generate 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.