Home > Backend Development > PHP Tutorial > PHP 数组惯用操作

PHP 数组惯用操作

WBOY
Release: 2016-06-13 13:14:51
Original
876 people have browsed it

PHP 数组常用操作

#在数组尾部添加一个或多个元素,新元素索引从最小的没用过的数值开始计,返回新数组长度
$int=array_push($arr,'a','b');

#在数组开头插入一个或多个元素,键值以0从第一个元素处重新累加,原数值的键值保持不变,返回新数组长度
$int=array_unshift($arr,'t1','t2');

?

?

//返回数组最后一个元素值,并把最后一个元素从原数组中剔除
$value=array_pop($arr);


//返回数组头一个元素值,并把头一个元素从原数组中剔除.如果键是数字的,所有元素都将获得新的键,从 0 开始
$value=array_shift($arr);


//计算数组中所有数值型元素值的和
$int=array_sum(array('a',2,'b'));

?

//ksort() 函数按照键名对数组排序,为数组值保留原来的键

?

?

PHP数组键值互换函数:array_flip()

$trans?=?array("a"?=> 1,?"b"?=> 1,?"c"?=> 2);
$trans?=?array_flip($trans);
print_r($trans);
Array
(
????[1] => b
????[2] => c
)



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template