Home  >  Article  >  Backend Development  >  Detailed example of array array in php

Detailed example of array array in php

PHP中文网
PHP中文网Original
2017-10-27 08:52:391920browse

The Array function in PHP allows you to access and manipulate arrays. Supports simple arrays and multidimensional arrays. The PHP Array function is an integral part of PHP core. No installation is required to use these functions.

Definition and usage

array() function is used to create an array. In PHP, there are three types of arrays: numeric arrays - arrays with numeric ID keys associative arrays - arrays with specified keys, each key associated with a value, multidimensional arrays - array syntax that contains one or more arrays The syntax of numerical array: array(value1, value2, value3, etc.); The syntax of associative array: array(key=>value, key=>value, key=>value, etc.); Parameter description key regulations Key name (numeric value or string). value specifies the key value. Technical details Return value: Returns an array of parameters. PHP version: 4+ Change log: Since PHP 5.4, you can use short array syntax, using [] instead of array(). For example, use $cars=["Volvo","BMW"]; instead of $cars=array("Volvo","BMW"); to create an associative array named $age:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age[&#39;Peter&#39;] . " years old.";
?>

Traverse and print Values ​​of numeric arrays:

<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo "<br>";
}
?>

Traverse and print values ​​of associative arrays:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

Create multidimensional arrays:

<?php
// 一个二维数组
$cars=array
(
array("Volvo",100,96),
array("BMW",60,59),
array("Toyota",110,100)
);
?>
函数 描述
array() 创建数组。
array_change_key_case() 返回其键均为大写或小写的数组。
array_chunk() 把一个数组分割为新的数组块。
array_column() 返回输入数组中某个单一列的值。
array_combine() 通过合并两个数组(一个为键名数组,一个为键值数组)来创建一个新数组。
array_count_values() 用于统计数组中所有值出现的次数。
array_diff() 比较数组,返回两个数组的差集(只比较键值)。
array_diff_assoc() 比较数组,返回两个数组的差集(比较键名和键值)。
array_diff_key() 比较数组,返回两个数组的差集(只比较键名)。
array_diff_uassoc() 比较数组,返回两个数组的差集(比较键名和键值,使用用户自定义的键名比较函数)。
array_diff_ukey() 比较数组,返回两个数组的差集(只比较键名,使用用户自定义的键名比较函数)。
array_fill() 用给定的键值填充数组。
array_fill_keys() 用给定的指定键名的键值填充数组。
array_filter() 用回调函数过滤数组中的元素。
array_flip() 反转/交换数组中的键名和对应关联的键值。
array_intersect() 比较数组,返回两个数组的交集(只比较键值)。
array_intersect_assoc() 比较数组,返回两个数组的交集(比较键名和键值)。
array_intersect_key() 比较数组,返回两个数组的交集(只比较键名)。
array_intersect_uassoc() 比较数组,返回两个数组的交集(比较键名和键值,使用用户自定义的键名比较函数)。
array_intersect_ukey() 比较数组,返回两个数组的交集(只比较键名,使用用户自定义的键名比较函数)。
array_key_exists() 检查指定的键名是否存在于数组中。
array_keys() 返回数组中所有的键名。
array_map() 将用户自定义函数作用到给定数组的每个值上,返回新的值。
array_merge() 把一个或多个数组合并为一个数组。
array_merge_recursive() 递归地把一个或多个数组合并为一个数组。
array_multisort() 对多个数组或多维数组进行排序。
array_pad() 将指定数量的带有指定值的元素插入到数组中。
array_pop() 删除数组中的最后一个元素(出栈)。
array_product() 计算数组中所有值的乘积。
array_push() 将一个或多个元素插入数组的末尾(入栈)。
array_rand() 从数组中随机选出一个或多个元素,返回键名。
array_reduce() 通过使用用户自定义函数,迭代地将数组简化为一个字符串,并返回。
array_replace() 使用后面数组的值替换第一个数组的值。
array_replace_recursive() 递归地使用后面数组的值替换第一个数组的值。
array_reverse() 将原数组中的元素顺序翻转,创建新的数组并返回。
array_search() 在数组中搜索给定的值,如果成功则返回相应的键名。
array_shift() 删除数组中的第一个元素,并返回被删除元素的值。
array_slice() 返回数组中的选定部分。
array_splice() 把数组中的指定元素去掉并用其它值取代。
array_sum() 返回数组中所有值的和。
array_udiff() 比较数组,返回两个数组的差集(只比较键值,使用一个用户自定义的键名比较函数)。
array_udiff_assoc() 比较数组,返回两个数组的差集(比较键名和键值,使用内建函数比较键名,使用用户自定义函数比较键值)。
array_udiff_uassoc() 比较数组,返回两个数组的差集(比较键名和键值,使用两个用户自定义的键名比较函数)。
array_uintersect() Compares arrays and returns the intersection of two arrays (only compares key values, using a user-defined key comparison function).
array_uintersect_assoc() Compare arrays and return the intersection of two arrays (compare key names and key values, use built-in functions to compare key names, use user-defined functions Compare key values).
array_uintersect_uassoc() Compare arrays and return the intersection of the two arrays (compare key names and key values, using two user-defined key name comparison functions).
array_unique() Remove duplicate values ​​from the array.
array_unshift() Insert one or more elements at the beginning of the array.
array_values() Returns all the values ​​in the array.
array_walk() Apply a user function to each member of the array.
array_walk_recursive() Applies a user function recursively to each member of an array.
arsort() Sort the associative array in descending order by key value.
asort() Sort the associative array in ascending order by key value.
compact() Creates an array containing variable names and their values.
count() Returns the number of elements in the array.
current() Returns the current element in the array.
each() Returns the current key/value pair in the array.
end() Point the internal pointer of the array to the last element.
extract() Import variables from the array into the current symbol table.
in_array() Checks whether the specified value exists in the array.
key() Get the key name from the associative array.
krsort() Sort the associative array in descending order by key name.
ksort() Sort the associative array in ascending order by key name.
list() Assign the values ​​in the array to some array variables.
natcasesort() Use the "natural sorting" algorithm to sort the array in a case-insensitive manner.
natsort() Sort the array using the "natural sorting" algorithm.
next() Move the internal pointer in the array backward one position.
pos() Alias ​​for current().
prev() Rewind the internal pointer of the array by one bit.
range() Creates an array containing elements in the specified range.
reset() Point the internal pointer of the array to the first element.
rsort() Sort the numeric array in descending order.
shuffle() Rearrange the elements in the array in random order.
sizeof() An alias for count().
sort() Sort the numeric array in ascending order.
uasort() Use a user-defined comparison function to sort the key values ​​in the array.
uksort() Use a user-defined comparison function to sort the key names in the array.
usort() Sort the array using a user-defined comparison function.

The above is the detailed content of Detailed example of array array in php. For more information, please follow other related articles on the PHP Chinese website!

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