PHP: Detailed explanation of array data type examples

怪我咯
Release: 2023-03-07 15:46:01
Original
4192 people have browsed it

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'......)
Copy after login

or

$array[key]='value'
Copy after login

or

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

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

<?php
$array = array(
   "foo" => "bar",
   "bar" => "foo",
);
Copy after login

// Since PHP 5.4, it can be like this

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

An example of the definition of an array

All of them are in the correct format

<?php

$arr1=array(&#39;This&#39;,&#39;is&#39;,&#39;a&#39;,&#39;example&#39;);

$arr2=array(0=>&#39;PHP&#39;,1=>&#39;中文网&#39;);

$arr3[0]=&#39;tmpname&#39;;

?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template