PHP数组访问

ArrayAccess 接口

平常使用的 Laravel 框架的模型对象既可以使用箭头来进行赋值, 又可以使用数组方式进行赋值.

1
2
3
4
5
6
<?php

$user = new Users();

$user->name = "RandyChan";
$user['email'] = "M17607475471@163.com";

通过以上两种方式都能给对象赋值, 这个功能主要是实现了 ArrayAccess 接口和 __get__set 这两个魔术方法.

ArrayAccess

实现 ArrayAccess 接口需要实现四个方法.

  • offsetExists(mixed $offset): boolean : 检查一个偏移位置是否存在
  • offsetGet(mixed $offset): mixed: 获取一个偏移位置的值
  • offsetSet(mixed $offset, mixed $value): void: 设置一个偏移位置的值
  • offsetUnset(mixed $offset): 复位一个偏移位置的值

其中 $offset 参数代表数组的键, $value 参数代表数组的值

实现 ArrayAccess 接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

class User implements \ArrayAccess
{
/**
* 声明一个数组存储数据
* @var array
*/
private $data = [];

/**
* 检查一个偏移位置是否存在, 只需判断这个键是否存在定义好的data变量里面
* @param mixed $offset
* @return bool|mixed
*/
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}

/**
* 获取一个偏移位置的值, 实现该方法只需要返回 data 数组对应的值就行
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->data[$offset];
}

/**
* 设置一个偏移位置的值, 往 data 里面设置就行
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}

/**
* 复位一个偏移位置的值, 删除数组里面的值, 这里先判断一下值是否存在
* @param mixed $offset
*/
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
}
}

/**
* 当设置一个不存在的属性时触发
* @param mixed $offset
* @param mixed $value
*/
public function __set($offset, $value)
{
$this->data[$offset] = $value;
}

/**
* 当获取一个不存在的属性是触发
* @param mixed $offset
* @return mixed
*/
public function __get($offset)
{
return $this->data[$offset];
}
}

$user = new User();
$user['name'] = "RandyChan";
$user->email = "M17607475471@163.com";

echo $user->name . "\n" . $user['email'];

执行结果

1
2
RandyChan
M17607475471@163.com

PHP实现冒泡排序

冒泡排序 (Bubble Sort)

冒泡排序(英语:Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。

演示动画:
冒泡排序

PHP代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

/**
* main
* @return void
*/
function main()
{
$array = [3, 1, 4, 5, 8, -1, 7, 6, 4, 2, 3];
bubbleSort($array, count($array) - 1);
fwrite(STDOUT, "排序结果: " . PHP_EOL);
foreach ($array as $value) {
fwrite(STDOUT, $value . PHP_EOL);
}
}

/**
* 冒泡排序
* @param $array
* @param $length
* @return void
*/
function bubbleSort(array &$array, int $length)
{
for ($i = 0; $i < $length; ++$i) {
for ($j = 0; $j < $length - $i; ++$j) {
if ($array[$j] > $array[$j + 1]) {
swap($array[$j], $array[$j + 1]);
}
}
}
}

/**
* 交换两个值
* @param mixed $firstVariable
* @param mixed $lastVariable
* @return void
*/
function swap(&$firstVariable, &$lastVariable)
{
$temp = $firstVariable;
$firstVariable = $lastVariable;
$lastVariable = $temp;
}

main();

PHP实现选择排序

排序算法(Selection Sort)

选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。

动画演示:

选择排序

PHP代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

/**
* main
* @return void
*/
function main()
{
$array = [3, -1, 4, 5, 8, 7, 9, 6, 4, 2, 3];
selectionSort($array, count($array));

fwrite(STDOUT, "排序结果: \n");
foreach ($array as $value) {
fwrite(STDOUT, $value . "\n");
}
}

/**
* 选择排序
* @param array $array
* @param int $length
* @return void
*/
function selectionSort(array &$array, int $length)
{
for ($i = 0; $i < $length - 1; $i ++) { // 由于每次都是和最后面的数字进行比较,所以最后一位不需要循环
$min = $i;
for ($j = $i + 1; $j < $length; $j ++) { // 循环未排序号的数字
if ($array[$j] < $array[$min]) {
$min = $j;
}
}
swap($array[$i], $array[$min]);
}
}

/**
*
* @param mixed $firstVariable
* @param mixed $lastVariable
*/
function swap(&$firstVariable, &$lastVariable)
{
$temp = $firstVariable;
$firstVariable = $lastVariable;
$lastVariable = $temp;
}

main();
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×