Home  >  Article  >  Backend Development  >  PHP: Detailed explanation of array data type examples

PHP: Detailed explanation of array data type examples

怪我咯
怪我咯Original
2017-04-27 17:05:323995browse

This chapter learns about the two composite data types in PHP Arrays

What is an array?

An array is a group A collection of data, which organizes a series of data to form an operable whole. Arrays can contain a lot of data, such as scalar arrays, arrays, objects, resources, and other syntax structures supported in PHP.

Each data in the array is called an element. The element includes two parts: index (key name) and value. The index of the element can be composed of a number or a string. The value of the element can be of multiple data types. The syntax format for defining an array is as follows

You can use the array() language structure to create a new array. Use commas to separate each array element

$array=('value1','value2'......)

or

$array[key]='value'

or

$array=array(key1=>value1,key2=>value2......)

Parameter details:

The parameter key is The subscript of the array element, value is the element corresponding to the array subscript.

Since 5.4, you can use the short array definition syntax, using [] instead of array().

In the past, the array was defined like this

 "bar",
   "bar" => "foo",
);

// Since PHP 5.4, it can be like this

$array = [
   "foo" => "bar",
   "bar" => "foo",
];
?>

An example of the definition of an array

All of them are in the correct format

'PHP',1=>'中文网');

$arr3[0]='tmpname';

?>

After creating an array, the number of elements in the array can be changed. As long as a value is assigned to the array, the length of the array will automatically increase. For more knowledge about arrays, please view the php Array (Array) topic
In the next section, we will explain the "#" of the two composite data types in PHP ##Object

The above is the detailed content of PHP: Detailed explanation of array data type examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn