search
HomeheadlinesDetailed explanation of php arrays

* An array is an ordered collection of key names and values ​​

* 1. Category:

* [Key name]

* 1.1 Index array [default]: The key name is an integer [it will be automatically converted if not]

* 1.2 Associative array: the key name is a string [can be converted into object properties]

* [Is the key value an array?]

* 1.1 One-dimensional array: The key value is a non-array type, the most commonly used

* 1.1 Multi-dimensional array: The key value is still an array, the most commonly used is a two-dimensional array, nesting is not recommended Too deep

* 2. Create:

* 2.1 Unified creation: $arr = [element1,element2,...];

* 2.2 Create one by one: $arr =[];$arr[]=element1;$arr[]=element2,...

* 3. Access: square brackets plus key name [key]

* 3.1 Overall: print_r($arr); var_dump($arr);

* 3.3 One by one: echo $array[key]: $array['name'];

* 4. Update:

* 4.1 Overall: It needs to be implemented through loop traversal: foreach($arr as $value){//...};

* 4.2 One by one: $arr[key] = new_value;

* 4.3 Clear or rebuild: $arr=[]; $arr = [1,2,3...]; Re-declaring it with the original name will overwrite the original array

* 5. Delete :

* 5.1 Overall: unset($arr);

* 5.2 Single deleted key name without rearrangement: unset($arr[key]);

* 5.3 Single deletion key name rearrangement [for index array]: array_splice($arr,$start,$count,[$newEle]);

* 5.4 Delete null value elements: array_filter($arr);

* 5.5 Delete specific elements: foreach if unset

//1.Create

$city = ['合肥','上海','杭州','南京']; //索引数组
$user = ['id'=>10,'name'=>'Peter','course'=>'php','grade'=>99]; //关联数组

//2.Access

print_r($user);  //整体输出
echo &#39;<hr>&#39;;
echo $user[&#39;name&#39;];  //查看单个元素
echo &#39;<hr>&#39;;

//3.Update

$user[&#39;name&#39;]=&#39;朱老师&#39;; //更新操作
echo $user[&#39;name&#39;];  //再次查看

//4.Delete

unset($city);  //删除整个数组
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($city); //查看不存在的变量会报错,加@符可忽略Notice级错误
echo &#39;<hr>&#39;;
unset($user[&#39;course&#39;]);  //删除单个元素
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($user);  //整体输出, $user[&#39;course&#39;]元素已经不存在了
echo &#39;<hr>&#39;;
$city = [&#39;合肥&#39;,&#39;上海&#39;,&#39;杭州&#39;,&#39;南京&#39;]; //索引数组
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($city);  //原始索引数组
echo &#39;<hr>&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
//array_splice(arr,start,end,preserve):从数组特定位置取出指定数量的元素
//返回取出的数据,取出的数据从原始数组中删除掉
//从$city的第二个元素开始,取出2个并返回它们
print_r(array_splice($city,1,2)); 
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
//再次查看,会发现取出的元素,已经从原始数组中消失了
print_r($city);  //整体输出
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!