Home  >  Article  >  Backend Development  >  Commonly used array operation functions in php (1/8)_PHP tutorial

Commonly used array operation functions in php (1/8)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:05886browse

An array is a set of elements that share certain characteristics, including similarity and type.
Each element is distinguished by a special identifier, called a key, and each key has a value
1. Two ways to create an array:
1.1 Use array() function

The code is as follows Copy code
 代码如下 复制代码

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

output
Alerk
Mary
Lucy
Bob
Jack
John
Mark

$usernames = array ('Alerk', 'Mary', 'Lucy', 'Bob', 'Jack', 'John', 'Mark' );

foreach ( $usernames as $name )
代码如下 复制代码

$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

{

echo $name . '
';
}

?>
 代码如下 复制代码

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

output
1
3
5
7
9

output

Alerk

Mary

Lucy Jack John Mark 1.2 Use range() function
The code is as follows Copy code
$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. Two ways to loop through array elements: 2.1 for loop
The code is as follows Copy code
'; } ?> output 1 3 5 7 9
1 2 3 4 5 6 7 8 http://www.bkjia.com/PHPjc/629237.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629237.htmlTechArticleAn array is a group of elements that have certain common characteristics, including similarity and type. Each element is distinguished by a special identifier, called a key, and each key has a value 1. Create a number...
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