Home > php教程 > php手册 > body text

php中常用的数组操作函数(1/8)

WBOY
Release: 2016-06-13 10:14:33
Original
896 people have browsed it

数组是一组有某种共同特性的元素,包括相似性和类型。
每个元素由一个特殊的标识符来区分,称之为key,而每个key都有一个value
1.创建数组的两种方式:
1.1 用array()函数

 代码如下 复制代码

$usernames = array ('Alerk', 'Mary', 'Lucy', 'Bob', 'Jack', 'John', 'Mark' );
foreach ( $usernames as $name )
{
echo $name . '
';
}
?>

output
Alerk
Mary
Lucy
Bob
Jack
John
Mark

1.2 用range()函数

 代码如下 复制代码

$numbers = range ( 0, 10 );
foreach ( $numbers as $num )
{
echo $num . '
';
}
$letters = range ( 'a', 'z' );
foreach ( $letters as $letter )
{
echo $letter . '
';
}
?>

output
0
1
2
3
4
5
6
7
8
9
10
a

c
d
e
f
g
h
i
j
k
l
m

o

q
r

t
u
v
w
x
y
z

2.循环访问数组元素的两种方式:
2.1 for循环

 代码如下 复制代码

//range的第三个参数表示步长
$numbers = range(1,10,2);
for($i = 0;$i {
echo $numbers[$i].'
';
}
?>

output
1
3
5
7
9

1 2 3 4 5 6 7 8

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!