转载 PHP 数组(一)
转载 PHP 数组(1)
原文:http://batwyx.iteye.com/blog/343090
PHP数组是一个重要的概念,它包含有大量的函数,方便人们的开发…现将它的数组分类,以方便查询及应用.
先说说PHP数组的定义…PHP数组包含两个项,key和value,可以通过key来获取相应的value,其中key又可以是数值和关联的,如$array[0],$array[one]…
创建数组
PHP中的数组声明跟其它语言的也有点小小的差别,但一样可以声明为一维,两维,三维及多维等,如
$array[0] = 1,$array = array(1,2,3); 一维数组,只包括三个值,属于数值型数组,引用时可用$array[0]来代表1,创建数值数组时可以省略索引.
$array = array(
?? 1 => “one”,
?? 2 => “two”,
?? 3 => “three”,
?? 4 => array(
??? “one” => 1,
??? “two” => 2,
??? “three” => 3
?? )
);
二维数组,同时又是关联数组,引用时可以$array[4][“one”]来代表1.
三维以上依此类推…
如果要批量创建数组,则可以通过下面这个函数:
array range ( mixed low, mixed high [, number step] )
如$array = range(1,6);代表array(1,2,3,4,5,6);
?? $array = range(a,f); 代表 array(a,b,c,d,e,f);
输出数组
PHP中输出数组的函数有比较多,常用的有
bool print_r ( mixed expression [, bool return] )
void var_dump ( mixed expression [, mixed expression [, ...]] )
还有像echo,print,printf都可以输出单个数组.
测试数组
有时我们需要判定一个变量是否为数组,则可以使用:
bool is_array ( mixed var )
增加或删除数组元素
数组声明后并不是一成不变的,可能通过对数组的增加删除来进行深入的操作:
int array_push ( array &array, mixed var [, mixed ...] ) 将一个或多个单元压入数组的末尾,数组的长度根据入栈变量的数目增加,如array_push($array,$var)
mixed array_pop ( array &array ) 将数组的最后一个元素弹出(出栈),并在结束后重置数组的指针
mixed array_shift ( array &array ) 返回数组的第一个元素.
int array_unshift ( array &array, mixed var [, mixed ...] ) 在数组的开头插入一个或多个单元
array array_pad ( array input, int pad_size, mixed pad_value ) 用值将数组填补到指定的长度,如array_pad($array,3,$var);
定位数组元素
bool in_array ( mixed needle, array haystack [, bool strict] ) 检查数组中是否存在某个值
array array_keys ( array input [, mixed search_value [, bool strict]] ) 返回数组中的所有键名,重组成一个新数组
bool array_key_exists ( mixed key, array search ) 检查给定的key是否存在于数组中.
array array_values ( array input ) 返回数组中所有的值
mixed array_search ( mixed needle, array haystack [, bool strict] ) 在数组中搜索给定的值,成功则返回key.
遍历数组
PHP中提供了很多获取key和value的函数
mixed key ( array &array ) 从关联数组中取得键名
mixed reset ( array &array ) 将数组指针重置
array each ( array &array ) 返回数组中的键/值对并将数组向前移一步
mixed current ( array &array ) 返回数组中的当前单元
mixed end ( array &array ) 将数组中的指针移向最后一位
mixed next ( array &array ) 将数组中的指针移向下一位
mixed prev ( array &array ) 将数组中的指针移向上一位
array array_reverse ( array array [, bool preserve_keys] ) 返回一个单元顺序相反的数组
array array_flip ( array trans ) 将数组中的键值角色调换
除了上面的函数外还可以使用循环来对数组中的元素进行遍历,如
foreach (array_expr as $value)
{ statement??? }
foreach (array_expr as $key=>$value)
{ statement?? }
提取每个键/值对,直到获得所有项或满足某些内部条件为止
void list ( mixed varname, mixed ... ) 把数组中的值赋给一些变量
确定数组大小和唯一性
int count ( mixed var [, int mode] ) 计算数组中单元数组或对象中属性的个数, sizeof 的同名函数
array array_count_values ( array input ) 统计数组中所有值出现的次数
array array_unique ( array array ) 移除数组中重复的值
数组排序
这个听说是计算器的核心问题…呵呵…事实也是这样…
bool sort ( array &array [, int sort_flags] ) 对数组进行排序
bool natsort ( array &array ) 用自然排序法对数组进行排序
bool natcasesort ( array &array ) 用自然排序法对数组进行排序,不区分大小写
bool rsort ( array &array [, int sort_flags] ) 对数组进行逆向排序
bool asort ( array &array [, int sort_flags] ) 对数组进行排序并保持索引关系
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] ) 对多个数组或多维数组进行排序
bool arsort ( array &array [, int sort_flags] ) 对数组进行逆序排序并保持索引关系
bool ksort ( array &array [, int sort_flags] ) 对数组按键名排序
bool krsort ( array &array [, int sort_flags] ) 对数组按键名逆序排序
合并,拆分,接合和分解数组
array array_combine ( array keys, array values ) 创建一个数组,一个数组的值作为其键名,另一个数组的值作为其值
array array_merge ( array array1 [, array array2 [, array ...]] ) 合并一个或多个数组
array array_merge_recursive ( array array1 [, array ...] ) 递归地全部一个或多个数组
array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) 从数组中取出一段,建立一个新的数组,如果offset为正数,拆分从距数组开关的offset位置开始,如果为负数,则拆分从距数组末尾的offset 位置开始,此时距数组开关的count(input_array)-|length|位置结束
array array_splice ( array &input, int offset [, int length [, array replacement]] ) 把数组中的部分值去掉,并用其它值替代.offset设置同上
array array_intersect ( array array1, array array2 [, array ...] ) 计算数组的交集,即是说如果第一个数组中出现过的值在接下来的几个数组中都有出现,则取出该值
array array_intersect_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的交集
array array_intersect_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的交集
array array_diff ( array array1, array array2 [, array ...] ) 计算数组的差集, 即是说跟第一个数组中不同的值
array array_diff_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的差集
array array_diff_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的差集
其它比较有用的数组函数
数组函数还有好多没有列出来…再上几个比较有用也比较常的,其它的就参考手册啦…手册里很清楚
mixed array_rand ( array input [, int num_req] ) 数组中随机取出一个或多个键,num指定个数
bool shuffle ( array &array ) 将数组打乱
number array_sum ( array array ) 计算数组中所有值的总和,关联数组忽略
array array_chunk ( array input, int size [, bool preserve_keys] ) 将一个数组分割成几个

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

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

Detailed explanation of the method of converting int type to byte in PHP In PHP, we often need to convert the integer type (int) to the byte (Byte) type, such as when dealing with network data transmission, file processing, or encryption algorithms. This article will introduce in detail how to convert the int type to the byte type and provide specific code examples. 1. The relationship between int type and byte In the computer field, the basic data type int represents an integer, while byte (Byte) is a computer storage unit, usually 8-bit binary data

In C++, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods. Implicit conversions The compiler performs implicit type conversions automatically. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things

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

The value range of int32 is from -2 to the 31st power to 2 to the 31st power minus 1, that is, -2147483648 to 2147483647. int32 is a signed integer type, which means it can represent positive numbers, negative numbers, and zero. It uses 1 bit to represent the sign bit, and the remaining 31 bits are used to represent the numerical value. Since one bit is used to represent the sign bit, the effective number of int32 bits is 31.

In Java, int is a 32-bit signed data type, and its variables require 32-bit memory; the valid range of the int data type is -2147483648 to 2147483647, and all integers in this range are called integer literals. An integer literal can be assigned to an int variable, such as "int num1 = 21;".

The number of bytes occupied by the int type may vary in different programming languages and different hardware platforms. Detailed introduction: 1. In C language, the int type usually occupies 2 bytes or 4 bytes. In 32-bit systems, the int type occupies 4 bytes, while in 16-bit systems, the int type occupies 2 bytes. In a 64-bit system, the int type may occupy 8 bytes; 2. In Java, the int type usually occupies 4 bytes, while in Python, the int type has no byte limit and can be automatically adjusted, etc.

What is AMP Coin? The AMP token was created by the Synereo team in 2015 as the main trading currency of the Synereo platform. AMP token aims to provide users with a better digital economic experience through multiple functions and uses. Purpose of AMP Token The AMP Token has multiple roles and functions in the Synereo platform. First, as part of the platform’s cryptocurrency reward system, users are able to earn AMP rewards by sharing and promoting content, a mechanism that encourages users to participate more actively in the platform’s activities. AMP tokens can also be used to promote and distribute content on the Synereo platform. Users can increase the visibility of their content on the platform by using AMP tokens to attract more viewers to view and share

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
