PHP 中的数组实际上是一个有序映射。映射是一种把 values 关联到 keys 的类型。此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合,栈,队列以及更多可能性。由于数组元素的值也可以是另一个数组,树形结构和多维数组也是允许的。
array
(PHP 4, PHP 5)
array — 新建一个数组
说明
array array ([ mixed $... ] )
返回根据参数建立的数组。参数可以用 => 运算符给出索引。关于数组是什么的信息请阅读数组一节。
Note:
array() 是一个语言结构,用于字面上表示数组,不是常规的函数。
语法“index => values”,用逗号分开,定义了索引和值。索引可以是字符串或数字。如果省略了索引,会自动产生从 0 开始的整数索引。如果索引是整数,则下一个产生的索引将是目前最大的整数索引 + 1。注意如果定义了两个完全一样的索引,则后面一个会覆盖前一个。
在最后一个定义的数组项目之后加一个逗号虽然不常见,却是合法的语法。
下面的例子演示了怎样建立一个二维数组,怎样给相应的数组指定键名,以及怎样在普通数组中略过和继续数字索引。
Example #1 array() 例子
<?php $fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third") ); ?>
Example #2 array() 的自动索引
<?php $array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13); print_r($array); ?>
以上例程会输出:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 13
[4] => 1
[8] => 1
[9] => 19
)
注意索引 3 被定义了两次,保留了最后的值 13。索引 4 在 索引 8 之后定义,下一个自动生成的索引(值为 19 那个)为 9,因为最大的索引是 8。
本例建立了从 1 开始的数组。
Example #3 从 1 开始索引的 array()
<?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); ?>
以上例程会输出:
Array ( [1] => January [2] => February [3] => March )
在 Perl 中,可以访问在双引号内的数组的值。但在 PHP 中需要将数组用花括号括起来。
Example #4 访问双引号内的数组
<?php $foo = array('bar' => 'baz'); echo "Hello {$foo['bar']}!"; // Hello baz! ?>
PHP Array 函数
PHP:指示支持该函数的最早的 PHP 版本。
函数
描述
PHP
array()
创建数组。
3
array_change_key_case()
返回其键均为大写或小写的数组。
4
array_chunk()
把一个数组分割为新的数组块。
4
array_combine()
通过合并两个数组来创建一个新数组。
5
array_count_values()
用于统计数组中所有值出现的次数。
4
array_diff()
返回两个数组的差集数组。
4
array_diff_assoc()
比较键名和键值,并返回两个数组的差集数组。
4
array_diff_key()
比较键名,并返回两个数组的差集数组。
5
array_diff_uassoc()
通过用户提供的回调函数做索引检查来计算数组的差集。
5
array_diff_ukey()
用回调函数对键名比较计算数组的差集。
5
array_fill()
用给定的值填充数组。
4
array_filter()
用回调函数过滤数组中的元素。
4
array_flip()
交换数组中的键和值。
4
array_intersect()
计算数组的交集。
4
array_intersect_assoc()
比较键名和键值,并返回两个数组的交集数组。
4
array_intersect_key()
使用键名比较计算数组的交集。
5
array_intersect_uassoc()
带索引检查计算数组的交集,用回调函数比较索引。
5
array_intersect_ukey()
用回调函数比较键名来计算数组的交集。
5
array_key_exists()
检查给定的键名或索引是否存在于数组中。
4
array_keys()
返回数组中所有的键名。
4
array_map()
将回调函数作用到给定数组的单元上。
4
array_merge()
把一个或多个数组合并为一个数组。
4
array_merge_recursive()
递归地合并一个或多个数组。
4
array_multisort()
对多个数组或多维数组进行排序。
4
array_pad()
用值将数组填补到指定长度。
4
array_pop()
将数组最后一个单元弹出(出栈)。
4
array_product()
计算数组中所有值的乘积。
5
array_push()
将一个或多个单元(元素)压入数组的末尾(入栈)。
4
array_rand()
从数组中随机选出一个或多个元素,并返回。
4
array_reduce()
用回调函数迭代地将数组简化为单一的值。
4
array_reverse()
将原数组中的元素顺序翻转,创建新的数组并返回。
4
array_search()
在数组中搜索给定的值,如果成功则返回相应的键名。
4
array_shift()
删除数组中的第一个元素,并返回被删除元素的值。
4
array_slice()
在数组中根据条件取出一段值,并返回。
4
array_splice()
把数组中的一部分去掉并用其它值取代。
4
array_sum()
计算数组中所有值的和。
4
array_udiff()
用回调函数比较数据来计算数组的差集。
5
array_udiff_assoc()
带索引检查计算数组的差集,用回调函数比较数据。
5
array_udiff_uassoc()
带索引检查计算数组的差集,用回调函数比较数据和索引。
5
array_uintersect()
计算数组的交集,用回调函数比较数据。
5
array_uintersect_assoc()
带索引检查计算数组的交集,用回调函数比较数据。
5
array_uintersect_uassoc()
带索引检查计算数组的交集,用回调函数比较数据和索引。
5
array_unique()
删除数组中重复的值。
4
array_unshift()
在数组开头插入一个或多个元素。
4
array_values()
返回数组中所有的值。
4
array_walk()
对数组中的每个成员应用用户函数。
3
array_walk_recursive()

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

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.