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 "[]".">

Home  >  Article  >  Backend Development  >  What are the ways to define arrays in php

What are the ways to define arrays in php

青灯夜游
青灯夜游Original
2021-05-18 17:48:504817browse

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