What are the ways to define arrays in php

青灯夜游
Release: 2023-03-09 18:48:01
Original
4761 people have browsed it

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

Example:

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

The running results are as follows shown.

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

Method 2:

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

Method 3:

$a[key1]=value1;
$a[key2]=value2;
Copy after login

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;
];
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!