Data types of arrays in php

Source: Internet
Author: User
In php, the array can store values of any PHP type. If no key name is specified for the given value, the maximum integer index value is used, the new key name is the value plus one, which is the php array.

In php, the array can store values of any PHP type. If no key name is specified for the given value, the maximum integer index value is used, the new key name is the value plus one, which is the php array.

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

Arrays in PHP are actually an ordered ING. valuing is a type that associates values with keys. this type has been optimized in many aspects, so it can be considered as a real array, or a list (vector), a hash list (an implementation of ING), a dictionary, a set, stack, queue, and more possibilities. the value of an array element can be another array. tree structure and multi-dimensional array are also allowed.

You can use the array () structure to create an array. it accepts any number of key => value pairs separated by commas.

The key can be integer or string. if the key is an integer, it is interpreted as an integer (for example, "8" is interpreted as 8, and "08" is interpreted as "08 "). the floating point number in the key is rounded to integer. in PHP, the index array and the associated array are the same. both of them can contain both the subscript of integer and string.

The value can be of any PHP type.

If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one. if the specified key name already has a value, the value will be overwritten.

Use TRUE as the key name to make integer 1 a key name. use FALSE as the key name to make integer 0 the key name. using NULL as the key name is equivalent to using a NULL string. use an empty string as the key name to create (or overwrite) a new value with an empty string as the key name, which is different from the empty square brackets.

Arrays and objects cannot be used as keys. this will cause a warning: Illegal offset type.

Create/modify with square brackets

If $ arr does not exist, a new one is created. this is also a replacement method for defining arrays. to change a value, just assign it a new value. if you want to delete a key/value pair, you need to use unset () for it ().

Note: If square brackets are provided but no key name is specified, the current maximum integer index value is used. the new key name is the value + 1. if no integer index exists, the key name is 0. if the specified key name already has a value, the value will be overwritten.

Note that the maximum integer key used here is not necessarily in the array currently. it only needs to exist after the index is re-generated in the last array.

Always add quotation marks on the array index represented by strings. for example, use $ foo ['bar'] instead of $ foo [bar]. but why is the error of $ foo [bar?

The reason is that this code has an undefined constant (bar) instead of a string ('bar'-note quotation marks), and PHP may define this constant later, unfortunately, your code has the same name. it can run because PHP automatically converts a bare string (a string without quotation marks and does not conform to any known symbol) into a normal string whose value is the bare string. for example, if no constant is defined as bar, PHP will replace it with 'bar' and use it.

In this example, the ID key is automatically assigned:

The instance code is as follows:

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

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:

Quagmire and Joe are Peter's neighbors

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.

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. "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. );

Summary

Arrays in php are often used in our development. for example, if you want to operate multiple data volumes and use array keys to save different values, arrays are the most efficient solution.

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.