value 1, key2=>value 2....)" statement; 3. Use the statement "$a[key1]=value 1;$a[key2]=value 2;..."; 4. Define it through square brackets "[]"."/> value 1, key2=>value 2....)" statement; 3. Use the statement "$a[key1]=value 1;$a[key2]=value 2;..."; 4. Define it through square brackets "[]".">
search
HomeBackend DevelopmentPHP ProblemWhat are the ways to define arrays in php

What are the ways to define arrays in php

May 18, 2021 pm 05:48 PM
phpdefine array

How to define an array: 1. Use the "array(value 1, value e2...)" statement; 2. Use "array(key1=>value 1,key2=>value 2.. ..)" statement; 3. Use the "$a[key1]=value 1;$a[key2]=value 2;..." statement; 4. Define it through square brackets "[]".

What are the ways to define arrays in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The array in PHP is actually an ordered mapping. A map is a type that associates values ​​to keys. This type is optimized in many ways, so it can be treated as a real array, or list (vector), hash table (which is an implementation of map), dictionary, set, stack, queue and many more possibilities. Since the value of an array element can also be another array, tree structures and multidimensional arrays are also allowed.

Generally speaking, the definition methods are as follows:

Method 1:

$a=array(value1,value2,value4,value5,value6...);

Example:

<?php 
$array=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;); 
$array[]=&#39;simon&#39;; 
print_r($array); 
?>

The running results are as follows shown.

Array  
 (  
   [0]=>a  
   [1]=>b  
   [2]=>c  
   [3]=>simon  
 )

Method 2:

$a=array(key1=>value1,key2=>value2,key3=>value3);

Method 3:

$a[key1]=value1;
$a[key2]=value2;

Method 4: Passed Brackets [] define the array

You can write like this after version 5.4 of PHP, with the new array abbreviation syntax.

php version 5.3 and previous versions do not accept writing like this...

$data = [
&#39;start_time&#39; => &#39;123&#39;,
&#39;end_time&#39; =>&#39;456&#39;
];

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the ways to define arrays in php. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft