PHP array basics tutorial

Source: Internet
Author: User
What is an array? During PHP development, you need to create many similar variables early or late, without many similar variables. you can store data as elements in an array,

What is an array?

During PHP development, you need to create many similar variables early or late, without many similar variables. you can store data as elements in an array, all elements in the array have their own IDs, so they can be easily accessed.

There are three types of arrays:

Value array: an array with the digit ID key. each element stored in the value array has a digit ID key.

Join array: each ID key in the array is associated with a value.

Multi-dimensional array: an array that contains one or more arrays.

You can use different methods to create a numeric array:

Numeric array:

Example 1: In this example, the ID key is automatically assigned. the code is as follows:

$ Names = array ("Peter", "Quagmire", "Joe ");

Example 2: In this example, we manually allocate the ID key. the code is as follows:

  1. $ Names [0] = "Peter ";
  2. $ Names [1] = "Quagmire ";
  3. $ Names [2] = "Joe"; these ID keys can be used in the script:
  4. $ Names [0] = "Peter ";
  5. $ Names [1] = "Quagmire ";
  6. $ Names [2] = "Joe ";
  7. Echo $ names [1]. "and". $ names [2]. "are". $ names [0]. "'s neighbors ";
  8. ?>
  9. // Output of the above code:
  10. // Quagmire and Joe are Peter's neighbors

Join array

Join array. each ID key is associated with a value.

It is not the best practice to use a numeric array to store data on specific named values. by associating an array, we can use values as keys and assign values to them.

Example 1: In this example, we use an array to allocate ages to different people. the code is as follows:

$ Ages = array ("Peter" => 32, "Quagmire" => 30, "Joe" => 34 );

Example 2: This example is the same as example 1, but shows another method for creating an array. the code is as follows:

  1. $ Ages ['Peter '] = "32 ";
  2. $ Ages ['quagmire'] = "30 ";
  3. $ Ages ['job'] = "34 ";

You can use the ID key in the script. the code is as follows:

  1. $ Ages ['Peter '] = "32 ";
  2. $ Ages ['quagmire'] = "30 ";
  3. $ Ages ['job'] = "34 ";
  4. Echo "Peter is". $ ages ['Peter ']. "years old .";
  5. ?>
  6. // Output of the above script:
  7. // Peter is 32 years old.

Multi-dimensional array

In multi-dimensional arrays, each element in the main array is also an array, and each element in the sub-array can also be an array, and so on.

Example1. in this example, we create a multidimensional array with an automatically assigned ID key. the code is as follows:

  1. $ Families = array
  2. (
  3. "Griffin in" => array
  4. (
  5. "Peter ",
  6. "Lois ",
  7. "Megan"
  8. ),
  9. "Quagmire" => array
  10. (
  11. "Glenn"
  12. ),
  13. "Brown" => array
  14. (
  15. "Cleveland ",
  16. "Loretta ",
  17. "Junior"
  18. )
  19. );
  20. /*
  21. If this array is output, it should be similar to the following:
  22. Array
  23. (
  24. [Griffin in] => Array
  25. (
  26. [0] => Peter
  27. [1] => Lois
  28. [2] => Megan
  29. )
  30. [Quagmire] => Array
  31. (
  32. [0] => Glenn
  33. )
  34. [Brown] => Array
  35. (
  36. [0] => Cleveland
  37. [1] => Loretta
  38. [2] => Junior
  39. )
  40. )
  41. */

Example 2: Let's try to display a single value in the above array. the code is as follows:

  1. Echo "Is". $ families ['grigging'] [2].
  2. "A part of the Griffin family? ";
  3. // Output of the above code:
  4. // Is Megan a part of the Griffin family?
Related Article

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.