Php array usage in PHP Tutorial 1

Source: Internet
Author: User
In php, arrays are a very important data type. they can store different types of data. compared with other simple integer types, arrays can only store a single data type, which is much better. next I will introduce php Data

In php, arrays are a very important data type. they can store different types of data. compared with other simple integer types, arrays can only store a single data, next I will introduce php array usage. PHP array is one of the most important knowledge. it can store one or more values in a single variable name.

What is an array?

Many similar variables need to be created during PHP development. 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:

Numeric array

Array with digit ID key

Join array

Each ID key in the array is associated with a value.

Multi-dimensional array

Array containing one or more arrays

Numeric array

Each element stored in the value array has a digital ID key. you can use different methods to create a value array:

Example 1

In this example, the ID key is automatically assigned:

The instance code is as follows:

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

Example 2

In this example, we manually allocate the ID key:

The instance code is as follows:

  1. $ Names [0] = "Peter ";
  2. $ Names [1] = "Quagmire ";
  3. $ Names [2] = "Joe ";
You can use these ID keys in the script:

The instance code is as follows:

  1. $ Names [0] = "Peter ";
  2. $ Names [1] = "Quagmire ";
  3. $ Names [2] = "Joe ";
  4. Echo $ names [1]. "and". $ names [2]. "are". $ names [0]. "'s neighbors ";
  5. ?>

Output of the above code:

1 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 related to specific named values. by associating arrays, 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 instance code is as follows:

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

Example 2

This example is the same as example 1, but shows another method to create an array:

The instance 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 instance 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. ?>

Output of the above script:

Peter is 32 years old.

Multi-dimensional array

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

Example 1

In this example, we create a multidimensional array with an automatically assigned ID key:

The instance code is as follows:

  1. $ Families = array
  2.  
  3. (
  4. "Griffin in" => array
  5. (
  6. "Peter ",
  7. "Lois ",
  8. "Megan"
  9. ),
  10. "Quagmire" => array
  11. (
  12. "Glenn"
  13. ),
  14. "Brown" => array
  15. (
  16. "Cleveland ",
  17. "Loretta ",
  18. "Junior"
  19. )
  20. );

If this array is output, it should be similar to the following:

  1. Array
  2. (
  3. [Griffin in] => Array
  4. (
  5. [0] => Peter
  6. [1] => Lois
  7. [2] => Megan
  8. )
  9. [Quagmire] => Array
  10. (
  11. [0] => Glenn
  12. )
  13. [Brown] => Array
  14. (
  15. [0] => Cleveland
  16. [1] => Loretta
  17. [2] => Junior
  18. )
  19. )

Example 2

Let's try to display a single value in the above array:

The instance code is as follows: 
  1. Echo "Is". $ families ['grigging'] [2].
  2. "A part of the Griffin family? ";

Output of the above code:

Is Megan a part of the Griffin family?

For PHP content, I would like to share it with you briefly. I hope you will be helpful when modifying the WordPress theme. lei Zi is also learning and doing it. he does not know much about it in many places and hopes to improve himself.

Display of array elements

In the above example, no matter $ people [2] or $ minutes les ['CS '], only an array element value with a known clear position is output, how to quickly output all or part of the array elements is undoubtedly the fastest way to use loop statements.

The instance code is as follows: 
  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. For ($ I = 0; $ I <4; $ I ++)
  3. Echo "$ people [$ I]";
  4. ?>

In addition to the for loop that understands the number of loops, you can also use a foreach statement that does not require the number of loops.

The instance code is as follows: 
  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. Foreach ($ people as $ xiangmu)
  3. Echo $ xiangmu;
  4. ?>

Array traversal

When using foreach for access, is the traversal order fixed? In what order does it traverse?

The instance code is as follows:

  1. $ Arr ['laruence '] = 'huixinchen ';
  2. $ Arr ['Yahoo '] = 2007;
  3. $ Arr ['baidu'] = 2008;
  4. Foreach ($ arr as $ key => $ val ){
  5. // What is the result?
  6. } <Li>

For example:

  1. $ Arr [2] = 'huixinchen ';
  2. $ Arr [1] = 2007;
  3. $ Arr [0] = 2008;
  4. Foreach ($ arr as $ key => $ val ){
  5. // What is the current result?
  6. } <Li>

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.