PHP Beginner's Learning array declaration _php Tutorial

Source: Internet
Author: User
Tags vars
Arrayis a form of organizing a number of variables of the same type in an orderly manner, in order to deal with them conveniently. The collection of these ordered, homogeneous data elements is called an array. The following describes the array declarations in PHP.

First, an overview of arrays

1. The nature of arrays: managing and manipulating a set of variables, batch processing

2. Compound type when array (multiple can be stored)

3. You can store any length of data in an array, or you can store any type of data

4. Array can complete the function of other language data structure (list, queue, Stack, collection Class)

Ii. Classification of arrays

There are multiple cells in the array, (cells are called elements)

Each element (subscript [key] and value)

The element is accessed by subscript (key) when the element is accessed by a single

1. One-dimensional arrays, two-dimensional arrays, three-dimensional arrays ... multidimensional arrays (arrays of arrays, where other arrays are stored in the array)

There are two types of arrays in 2.PHP

Indexed array: The index of an ordinal integer that is the subscript

Associative array: Is the subscript is the string as the index

Subscript (integer, string) only these two types of

Three, array of multiple declaration methods

1. Assigning a direct array element to a value declaration

If index subscript is not given, it will be indexed sequentially starting from 0

If index subscript is given, the next will increase by 1 from the largest start

If the preceding subscript appears, the assignment is to reassign the previous element

When mixing declarations, indexes and associations do not affect each other (do not affect the declaration of index subscript)

2. Using the array () function declaration

The default is an indexed array, if you specify subscripts for associative arrays and indexed arrays, use the key = = value, use "," split between multiple members

3. Use a different function declaration

(1) Indexed array

 
  
  
  1. $user [0]=1; //user ordinal
  2. $user [1]= "Zhangsan" ; //user name
  3. $user [2]=10; //age
  4. $user [3]= "Nan" ; //sex
  5. echo '
     
/pre>
' ;

(2) Associative arrays

 
 
  1. < ' PHP
  2. $user ["id"]=1;
  3. $user ["name"]= "Zhangsan" ;
  4. $user ["age"]=10;
  5. $user ["Sex"];
  6. $user ["age"]=90; //Assigned value
  7. Echo $user["name"]; //Output
  8. //Use Array () to declare arrays
  9. $user = Array (1,"Zhangsan", ten,"nan");
  10. //Use Array () to declare an associative array
  11. $user = Array ("id"=>1,"name"="zhangsan","age" =>10,"Sex"="nan");
  12. //Declare multidimensional arrays (multiple records) to hold multiple user information records in a table
  13. $user = Array (
  14. call this line with $user[0], such as calling the name in the record, $user [0][1]
  15. Array (1,"Zhangsan", ten,"nan"),
  16. call this line with $user[1], such as calling the name in the record, $user [1][1]
  17. Array (2,"Lisi","NV")
  18. );
  19. //Arrays save multiple tables with multiple records per table
  20. $info = Array (
  21. "User" = Array (
  22. Array (1,"Zhangsan", ten,"nan"),
  23. Array (2,"Lisi","NV")
  24. ),
  25. "Score" = Array (
  26. Array (1,90,80,70),
  27. Array (2,60,40,70)
  28. )
  29. );
  30. Echo $info["Score"][1][1]; //output,
  31. ?>

Hope that through the introduction of this article, can bring you help.


http://www.bkjia.com/PHPjc/445774.html www.bkjia.com true http://www.bkjia.com/PHPjc/445774.html techarticle An array is a form in which a number of variables of the same type are organized in an orderly manner in order to deal with them conveniently. The set of these ordered, homogeneous data elements ...

  • 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.