PHP beginner's learning array declaration

Source: Internet
Author: User

ArrayIn programming, to facilitate processing, several variables of the same type are organized in an orderly manner. The collections of these sort-by-order data elements are called arrays. The following describes the array declaration in PHP.

I. Overview of Arrays

1. The essence of Arrays: managing and operating a group of variables, batch processing

2. array composite type (multiple objects can be stored)

3. Data of any length can be stored in an array or any type of data.

4. Arrays can be used to complete data structures in other languages (linked lists, queues, stacks, and collection classes)

Ii. array Classification

An array contains multiple units (elements)

Each element (subscript [Key] and value)

When accessing a single element, all elements are accessed by subscript (key ).

1. One-dimensional array, two-dimensional array, three-dimensional array... Multi-dimensional array (array, which contains other arrays)

2. PHP has two types of Arrays

Index Array: Index where the subscript is an ordered integer

Associated array: The subscript is a string as an index.

Only these two types of subscript (integer, string)

3. Multiple array declaration Methods

1. directly assign a value to an array element

If the index subscript is not given, the index starts from 0.

If an index subscript is given, the next index increases by 1 from the maximum.

If the subscript appears next to it, if it is a value assignment, it is to re-assign the value to the previous element.

During hybrid declaration, the index and association do not affect each other (the index bottom object declaration is not affected)

2. Use the array () function declaration

The default value is an index array. If you specify a subscript for the associated array and index array, use the key => value to separate multiple members using ","

3. Use other function declarations

(1) Index Array

 
 
  1. $ User [0] = 1; // user serial number
  2. $ User [1] = "zhangsan"; // user Name
  3. $ User [2] = 10; // age
  4. $ User [3] = "nan"; // gender
  5. Echo '<pre> ';
  6. Print_r ($ user );
  7. Echo '</pre> ';

(2) join an array

 
 
  1. <'Php
  2. $ User ["id"] = 1;
  3. $ User ["name"] = "zhangsan ";
  4. $ User ["age"] = 10;
  5. $ User ["sex"];
  6. $ User ["age"] = 90; // value assignment
  7. Echo $ user ["name"]; // output
  8. // Use array () to declare an array
  9. $ User = array (1, "zhangsan", 10, "nan ");
  10. // Declare the joined array Using array ()
  11. $ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
  12. // Declare multi-dimensional arrays (multiple records) to save multiple user information records in a table
  13. $ User = array (
  14. // Call this line with $ user [0]. For example, call the name in this record, $ user [0] [1]
  15. Array (1, "zhangsan", 10, "nan "),
  16. // Call this line with $ user [1]. For example, call the name in this record, $ user [1] [1]
  17. Array (2, "lisi", 20, "nv ")
  18. );
  19. // Multiple tables are saved in an array. Each table has multiple records.
  20. $ Info = array (
  21. "User" => array (
  22. Array (1, "zhangsan", 10, "nan "),
  23. Array (2, "lisi", 20, "nv ")
  24. ),
  25. "Score" => array (
  26. Array ),
  27. Array (2, 60, 40, 70)
  28. )
  29. );
  30. Echo $ info ["score"] [1] [1]; // output 60,
  31. ?>

I hope this article will help you.


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.