一.什么是数组
数组是一组有某种共同特性的元素,包括相似性和类型。
每个元素由一个特殊的标识符来区分,称之为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
2.2 foreach循环
复制代码 代码如下:
$letters = range('a','h',2);
foreach($letters as $letter)
{
echo $letter.'
';
}
?>
output
a
c
e
g
Foreach还可以用来输出数组的下标和对应的值
复制代码 代码如下:
$letters = range('a','g',2);
foreach($letters as $key => $value)
{
echo $key.'---'.$value.'
';
}
?>
output
0---a
1---c
2---e
3---g
3.is_array()函数,用于变量判断是否为一个数组
复制代码 代码如下:
$numbers = range(1,10,2);
if(is_array($numbers))
{
foreach($numbers as $num)
{
echo $num.'
';
}
}
else
{
echo $numbers;
}
?>
4.print_r函数,打印关于变量的易于理解的信息
复制代码 代码如下:
$usernames = array ('Jackie', 'Mary', 'Lucy', 'Bob', 'Mark', 'John' );
print_r ( $usernames );
?>
output
Array ( [0] => Jackie [1] => Mary [2] => Lucy [3] => Bob [4] => Mark [5] => John )
源代码里可以看到显示为:
Array
(
[0] => Jackie
[1] => Mary
[2] => Lucy
[3] => Bob
[4] => Mark
[5] => John
)
二.自定义键数组
1.如果不想创建默认下标为零的数组,可以用如下方法,创建键为字符串的数组
复制代码 代码如下:
//初始化数组
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);
//访问数组各元素
echo $userages['Jack'].'
';
echo $userages['Lucy'].'
';
echo $userages['Mark'].'
';
?>
2.往自定义键数组里追加元素
复制代码 代码如下:
//初始化数组
$ages = array('Jack'=>23);
//追加元素
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'----'.$value.'
';
}
?>
3.直接添加元素,无需创建数组。
复制代码 代码如下:
//不创建数组直接添加
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'----'.$value.'
';
}
?>
4.循环打印数组foreach的使用
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
foreach($ages as $key => $value)
{
echo $key.'=>'.$value.'
';
}
?>
5. each() -- 返回数组中当前的键/值对并将数组指针向前移动一步
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
$a = each($ages);
print_r($a);
echo '
';
$a = each($ages);
print_r($a);
echo '
';
$a = each($ages);
print_r($a);
?>
用each()函数做循环打印
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!! $element = each($ages))
{
print_r($element);
echo '
';
}
?>
另一种打印方式
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!! $element = each($ages))
{
echo $element['key'].'=>'.$element['value'];
echo '
';
}
?>
6.list()函数的使用--把数组中的值赋给一些变量
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
list($name,$age)= each($ages);
echo $name.'=>'.$age;
?>
用list循环打印结果
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
while(!!list($name,$age)= each($ages))
{
echo $name.'=>'.$age.'
';
}
?>
output
Jack=>23
Lucy=>25
Mark=>28
7.reset()函数的使用--将数组的内部指针指向第一个单元
复制代码 代码如下:
$ages['Jack']=23;
$ages['Lucy']=25;
$ages['Mark']=28;
each($ages);
each($ages);
list($name,$age)= each($ages);
echo $name.'=>'.$age.'
';
//把数组重新设定到数组开始处
reset($ages);
list($name,$age)= each($ages);
echo $name.'=>'.$age.'
';
?>
Output
Mark=>28
Jack=>23
8. array_unique() -- 移除数组中重复的值
复制代码 代码如下:
$nums = array(1,2,3,4,5,6,5,4,3,2,1,1,2,3,4,5,6);
//返回一个不包含重复值的数组
$result = array_unique($nums);
print_r($result);
?>
Output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
9. array_flip ()-- 交换数组中的键和值
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28);
$ages = array_flip($userages);
print_r($ages);
?>
output
Array ( [23] => Jack [25] => Lucy [28] => Mark )
三.数组里的数组
数组里不一定就是一个关键字和值的列表,数组里也可以放入数组
复制代码 代码如下:
$produces = array(
array('apple',6,28.8),
array('pear',3,15.6),
array('banana',10,4.6)
);
echo $produces[0][0].'|'.$produces[0][1].'|'.$produces[0][2].'
';
echo $produces[1][0].'|'.$produces[1][1].'|'.$produces[1][2].'
';
echo $produces[2][0].'|'.$produces[2][1].'|'.$produces[2][2].'
';
?>
output
apple|6|28.8
pear|3|15.6
banana|10|4.6
用for循环打印数组中的数组
复制代码 代码如下:
$produces = array (
array ('apple', 6, 28.8 ),
array ('pear', 3, 15.6 ),
array ('banana', 10, 4.6 )
);
for($i = 0; $i {
for($j = 0; $j {
echo '|' . $produces[$i][$j];
}
echo '
';
}
?>
output
|apple|6|28.8
|pear|3|15.6
|banana|10|4.6
二维数组
复制代码 代码如下:
$produces = array (
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ),
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ),
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 )
);
while(!!List($key,$value)=each($produces))
{
while(!!list($key2,$value2)=each($value))
{
echo '|'.$key2.'=>'.$value2;
}
echo '
';
}
?>
output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
用foreach来打印则更容易(推荐)
复制代码 代码如下:
$produces = array (
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ),
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ),
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 )
);
foreach($produces as $key1 => $value1)
{
foreach($value1 as $key2 => $value2)
{
echo '|'.$key2.'=>'.$value2;
}
echo '
';
}
?>
output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
四.数组的排序
1.Sort()函数对英文的排序
复制代码 代码如下:
$fruits = array('lemo','banana','apple','pear');
echo '原始的数组:';
print_r($fruits);
echo '
';
sort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [0] => lemo [1] => banana [2] => apple [3] => pear )
排序后的数组:Array ( [0] => apple [1] => banana [2] => lemo [3] => pear )
2.Sort()函数对中文的排序
复制代码 代码如下:
$fruits = array('柠檬','香蕉','苹果','梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
sort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
Output:
原始的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )
排序后的数组:Array ( [0] => 柠檬 [1] => 梨子 [2] => 苹果 [3] => 香蕉 )
3. asort -- 对数组进行排序并保持索引关系
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
asort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
排序后的数组:Array ( [a] => 柠檬 [d] => 梨子 [c] => 苹果 [b] => 香蕉 )
4. ksort -- 对数组按照键名排序
复制代码 代码如下:
$fruits = array('b'=>'柠檬','a'=>'香蕉','d'=>'苹果','c'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
ksort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [b] => 柠檬 [a] => 香蕉 [d] => 苹果 [c] => 梨子 )
排序后的数组:Array ( [a] => 香蕉 [b] => 柠檬 [c] => 梨子 [d] => 苹果 )
5. rsort -- 对数组逆向排序
复制代码 代码如下:
$fruits = array('柠檬','香蕉','苹果','梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
rsort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )
排序后的数组:Array ( [0] => 香蕉 [1] => 苹果 [2] => 梨子 [3] => 柠檬 )
6. arsort -- 对数组进行逆向排序并保持索引关系
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
arsort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
排序后的数组:Array ( [b] => 香蕉 [c] => 苹果 [d] => 梨子 [a] => 柠檬 )
7. krsort -- 对数组按照键名逆向排序
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
krsort($fruits);
echo '排序后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
排序后的数组:Array ( [d] => 梨子 [c] => 苹果 [b] => 香蕉 [a] => 柠檬 )
8. shuffle -- 将数组打乱
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
shuffle($fruits);
echo '打乱后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
打乱后的数组:Array ( [0] => 香蕉 [1] => 苹果 [2] => 柠檬 [3] => 梨子 )
9. array_reverse -- 返回一个单元顺序相反的数组
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
$fruits = array_reverse($fruits);
echo '反转后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
反转后的数组:Array ( [d] => 梨子 [c] => 苹果 [b] => 香蕉 [a] => 柠檬 )
10. array_unshift -- 在数组开头插入一个或多个单元
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
array_unshift($fruits,'杮子');
echo '插入后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
插入后的数组:Array ( [0] => 杮子 [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
11. array_shift -- 将数组开头的单元移出数组
复制代码 代码如下:
$fruits = array('a'=>'柠檬','b'=>'香蕉','c'=>'苹果','d'=>'梨子');
echo '原始的数组:';
print_r($fruits);
echo '
';
array_shift($fruits);
echo '移出后的数组:';
print_r($fruits);
?>
output
原始的数组:Array ( [a] => 柠檬 [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
移出后的数组:Array ( [b] => 香蕉 [c] => 苹果 [d] => 梨子 )
12. array_rand -- 从数组中随机取出一个或多个单元
复制代码 代码如下:
$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );
echo '原始的数组:';
print_r ( $fruits );
echo '
';
$newArr_key = array_rand ( $fruits, 2 );
echo '随机后的数组:';
echo $fruits [$newArr_key [0]].' ';
echo $fruits [$newArr_key [1]];
?>
output
原始的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )
随机后的数组:梨子 苹果
13. array_pop -- 将数组最后一个单元弹出(出栈)
复制代码 代码如下:
$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );
echo '原始的数组:';
print_r ( $fruits );
echo '
';
array_pop ( $fruits );
echo '弹出后的数组:';
print_r ( $fruits );
?>
Output:
原始的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )
弹出后的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 )
14. array_push -- 将一个或多个单元压入数组的末尾(入栈)
复制代码 代码如下:
$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );
echo '原始的数组:';
print_r ( $fruits );
echo '
';
array_push ( $fruits,'杮子');
echo '弹出后的数组:';
print_r ( $fruits );
?>
Output:
原始的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )
弹出后的数组:Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 [4] => 杮子 )
五.数组的指针的操作
each -- 返回数组中当前的键/值对并将数组指针向前移动一步
current -- 返回数组中的当前单元
reset -- 将数组的内部指针指向第一个单元
end -- 将数组的内部指针指向最后一个单元
next -- 将数组中的内部指针向前移动一位
pos -- current() 的别名
prev -- 将数组的内部指针倒回一位
复制代码 代码如下:
$fruits = array ('柠檬', '香蕉', '苹果', '梨子' );
print_r ( $fruits );
echo '
';
echo 'each() : ';
print_r ( each ( $fruits ) );
echo '
';
echo 'current() : ';
echo (current ( $fruits ));
echo '
';
echo 'next() : ';
echo (next ( $fruits ));
echo '
';
echo 'end() : ';
echo (end ( $fruits ));
echo '
';
echo 'prev() : ';
echo (prev ( $fruits ));
echo '
';
echo 'pos() : ';
echo (pos ( $fruits ));
echo '
';
?>
Output:
Array ( [0] => 柠檬 [1] => 香蕉 [2] => 苹果 [3] => 梨子 )
each() : Array ( [1] => 柠檬 [value] => 柠檬 [0] => 0 [key] => 0 )
current() : 香蕉
next() : 苹果
end() : 梨子
prev() : 苹果
pos() : 苹果
六.统计数组个数
count -- 计算数组中的单元数目或对象中的属性个数
sizeof -- count() 的别名
array_count_values -- 统计数组中所有的值出现的次数
复制代码 代码如下:
$nums = array (1, 3, 5, 1, 3, 4, 5, 65, 4, 2, 2, 1, 4, 4, 1, 1, 4, 1, 5, 4, 5, 4 );
echo count ( $nums );
echo '
';
echo sizeof ( $nums );
echo '
';
$arrayCount = array_count_values ( $nums );
print_r ( $arrayCount );
?>
output
22
22
Array ( [1] => 6 [3] => 2 [5] => 4 [4] => 7 [65] => 1 [2] => 2 )
七.将数组转换成标量变量:extract()
把数组中的每个元素转换成变量,变量名是数组元素的key,变量值为数组元素的value.
复制代码 代码如下:
$fruits = array('a'=>'apple','b'=>'banana','o'=>'orange');
extract($fruits);
echo $a.'
';
echo $b.'
';
echo $o.'
';
?>
output
apple
banana
orange

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function