


PHP Study Notes: Array Values and Array Traversal and Sorting_PHP Tutorial
本文章来给大家介绍一篇简单的php入门实例,这里主要是讲述了php数组值的操作及函数以及数组遍历与数组排序的实例,各位同学可进入参考。
数组值的操作
1. 值的析取
PHP中,用list来析取数组中的值,如list($a, $b) = $array。如果list中的值多于数组个数,list中多余的值会设为NULL。也可以用逗号来跳过数组中的值,如list($a, ,$b) = $array。
2.划分数组
如果想取得子数组,可以用array_slice(array, offset, length);来取得。它返回一个新的下标从0开始的数组。如果原数组的下标是字符串,好像是没有什么意义的,最好不要用,可以用array_splice来取得子串。
3.将数组分为多个数组
用array_chunk可以把数组分成一个二维数组。详细的可以通过链接看官方的说明。
4.键和值
array_keys($array),取得由数组索引组成的数组
array_value($array),取得由数组值组成的数组,索引从0开始重新分配。
array_key_exists($key, array),元素是否存在检查。
array_splice, 删除插入元素。
5.数组和变量之间的转换
extract(array) 把数组变成变量
compact() 把变量变成数组
6.数组的查找
in_array(array, ) 返回元素是否在数组中存在。
array_search() 返回被找到元素的索引。
7.整个数组函数
array_ sum() 计算数组的和。
array_ merge() 合并两个数组。
array_ diff() 两个数组之间的不同值。
array_ filter() 过滤元素
8.集合、堆栈、队列
array_ unique() 取两个数组的合集,如果值相同,保留前一个数组的索引。
array_ intersect() 取两个数级的交集,保留第一个数组的索引。
array_ push() 加入堆栈。
array_ pop() 弹出堆栈。
array_ shift() 加入队列。
array_ unshift() 弹出队列。
1.简单的遍历
PHP中,数组最简单的遍历方法莫过于for和foreach了。其中foreach有两种写法,一种只遍历值、另一种遍历索引和值。具体可以看如下代码。
代码如下 | 复制代码 |
$test01 = array('a', 'b', 'c'); // for for ($i = 0; $i < count($test01); $i++) { echo $test01[$i]; } // foreach value only foreach ($test01 as $value) { echo $value; } // foreach key and value $test01 = array('a' => 'aaaa', 'b' => 'bbbb', 'c' => 'cccc'); foreach ($test01 as $key => $value) { echo "$key => $value"; } |
2.迭代器遍历
PHP中,迭代遍历主要要用到以下函数。
current() 迭代的当前元素。
reset() 重新移动到第一个元素并返回它。
next() 移动到下一个元素并返回它。
prev() 移动到上一个元素并返回它。
end() 移动到最后一个元素并返回它。
each() 以数组的形式返回当前元素的索引和值,并移动到下一个迭代。
key() 返回当前的索引。
array_ walk() 为每一个元素调用函数。
array_ reduce() 为每一个元素依次计算。
代码如下 | 复制代码 |
$test02 = array(1, 2, 3, 4, 5); |
3. Sorting of arrays
In PHP, there are three sorting methods, sorting by index, sorting by value (without retaining the original index), and sorting by value (retaining the original index). Each type is divided into three functions: ascending order, descending order and user-defined order. They are as follows:
Sort by index: ①Ascending ksort() ②Descending krsort() ③User-defined order uksort()
Sorting without retaining the original index value: ① ascending sort() ② descending rsort() ③ user-defined order usort()
Keep the original index value sorting: ①Ascending asort() ②Descending arsort() ③User-defined order uasort()
In PHP, array_multisort can also be used to sort multiple arrays at once, but it may be used less often in projects.
Flip the array, flip the numerical index, and start the index from 0 again: array_reverse()
Swap index and value: array_flip()
Random order: shuffle()

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

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

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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 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 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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

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 have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
