PHP array functions (traversal, sorting)
1. Traverse
- foreach
foreach(array_expression as $value) 和 foreach(array_expression as $key=>$value)
Copy after login list()和each():list()仅能用于数字索引的数组,且数字索引从0开始。each()返回数组中的键名和对应的值,并向前移动数组指针。
Copy after login-
<?php $array=array( //定义数组 "0"=>"PHP24堂课", "1"=>"JAVA24堂课", "2"=>"VB24堂课", "3"=>"VC24堂课" ); while(list($name,$value)=each($array)){ //使用list函数获取each函数中返回数组的值,并分别赋给$name和$value,然后使用while循环输出 echo $name=$value."<br>"; //输出list函数获取到的键名和值 } ?>
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
二、常用函数
统计数组个数int count(mixed var)
向数组中添加元素:array_push()将传入的元素添加到数组的末尾,并返回数组新的单元总数
int array_push(array array,mixed var) //array为指定数组,var为压入数组中的值
获取数组中最后一个元素:array_pop()返回数组中的最后一个元素,并将数组长度减1,如果数组为空(或不是数组)则返回null。mixed array_pop(array array)
删除数组中重复元素:array array_unique(array array)
删除数组中某个元素:unset(mixed arr[*])
获取数组中指定元素的键名mixed array_search(mixed needle,array haystack[, bool strict]) //如果查询的元素在数组中出现两次以上,则返回第一个匹配的键名 //needle:指定数组中搜索的值 //haystach:指定被搜索的数组 //strict:可选参数,若为true,将在haystack中检查needle的类型
array array_keys(array input[, mixed search_value[, bool strict]]) //返回input数组中所有匹配的键名
三、排序
sort()实现数组从低到高排序;字符串型按ASCII码的顺序排序。
bool sort(array &array[, int sort_flags]) //sort_flags指定排序方式:SORT_REGULAR(默认),SORT_NUMERIC(将元素作为数字来比较),SORT_STRING(将元素作为字符串来比较)
rsort():用于实现对数组进行从高到低的排序。
The above introduces the PHP array functions (traversal, sorting), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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











The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

Uniapp is a cross-platform development framework. Its powerful cross-end capabilities allow developers to develop various applications quickly and easily. It is also very simple to implement drag-and-drop sorting and drag-and-drop operations in Uniapp, and it can support drag-and-drop operations of a variety of components and elements. This article will introduce how to use Uniapp to implement drag-and-drop sorting and drag-and-drop operations, and provide specific code examples. The drag-and-drop sorting function is very common in many applications. For example, it can be used to implement drag-and-drop sorting of lists, drag-and-drop sorting of icons, etc. Below we list

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Example In this example, we first look at the usage of list.sort() before continuing. Here, we have created a list and sorted it in ascending order using sort() method - #CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList .sort(

New numerical sorting function in PHP8.1 PHP is a widely used open source scripting language commonly used in web development. Not only is it powerful, it also has a rich built-in function library. In the recently released version of PHP 8.1, some interesting features and functions have been added, including numerical sorting functions. These new functions can make it easier for developers to sort numerical arrays, improving development efficiency and code readability. In past PHP versions, we usually used the sort() or rsort() function logarithm
