From 8de41a521b4bfd7a6cc1fd9ffdc1104636dfde43 Mon Sep 17 00:00:00 2001 From: pengshiyu <1940607002@qq.com> Date: Thu, 10 Mar 2022 22:41:03 +0800 Subject: [PATCH] fix --- blog/php-basic/array-function.md | 208 ++++++++++++++++++++++++++++--- blog/php-basic/index.md | 2 + blog/php-basic/thinking.md | 81 ++++++++++++ 3 files changed, 273 insertions(+), 18 deletions(-) create mode 100644 blog/php-basic/thinking.md diff --git a/blog/php-basic/array-function.md b/blog/php-basic/array-function.md index 216e37f..9ab588f 100644 --- a/blog/php-basic/array-function.md +++ b/blog/php-basic/array-function.md @@ -1,6 +1,5 @@ # 数组相关函数 - 自定义数组打印函数,便于查看 ```php @@ -15,7 +14,7 @@ function print_array($array){ ## 排序函数 -按照ASCII码排序 +按照 ASCII 码排序 `sort` 对 array 本身按照值(value)升序排序。(下标重排) @@ -52,7 +51,6 @@ print_array($arr); // 2 => Jack ``` - `asort` 对 array 自身按照升序进行排序(下标保留) ```php @@ -87,7 +85,6 @@ print_array($arr); // 1 => Jack ``` - `ksort` 对 array 本身进行按键(key)升序排序。 ```php @@ -141,21 +138,196 @@ print_array($arr); ## 指针函数 -reset -end -next -prev -current -key +reset 将 array 的内部指针倒回到第一个单元并返回第一个数组单元的值。 + +```php +reset(array|object &$array): mixed +``` + +end 将 array 的内部指针移动到最后一个单元并返回其值。 + +```php +end(array|object &$array): mixed +``` + +next 将数组中的内部指针向前移动一位 + +```php +next(array|object &$array): mixed +``` + +prev 将数组的内部指针倒回一位 + +```php +prev(array|object &$array): mixed +``` + +current 返回数组中的当前值 + +```php +current(array|object $array): mixed +``` + +key 返回数组中当前单元的键名。 + +```php +key(array|object $array): int|string|null +``` + +> 注意:next 和 prev 移动指针,可能移出数组,只能通过 end 或者 reset 重置指针 + +示例 + +```php + +$arr = [1, 3, 5, 7, 9]; + +echo next($arr); +echo next($arr); +echo prev($arr); +echo current($arr); +echo key($arr); +echo reset($arr); +echo end($arr); +// 3533119 +``` ## 其他函数 -count -array_push() -array_pop -array_reverse -in_array -array_keys -array_values +count 统计数组、Countable 对象中所有元素的数量 + +```php +count(Countable|array $value, int $mode = COUNT_NORMAL): int + +// eg: +$arr = [1, 2, 3, 4, 5]; + +echo count($arr); +// 5 +``` + +array_push 将 array 当成一个栈,并将传入的变量压入 array 的末尾。 + +```php +array_push(array &$array, mixed $value1, mixed $... = ?): int + +// 和如下效果相同: +$array[] = $var; +``` + +array_pop 弹出并返回 array 最后一个元素的值 + +```php +array_pop(array &$array): mixed +``` + +array_shift 将 array 的第一个单元移出并作为结果返回 + +```php +array_shift(array &$array): mixed +``` + +array_unshift 在数组开头插入一个或多个单元 + +```php +array_unshift(array &$array, mixed ...$values): int +``` + +数据结构 + +- 栈 压栈,FILO先进后出 +- 队列 排队,FIFO 先进先出 + +实现栈: +- 前面 array_unshift/array_shift +- 后面 array_push/array_pop + +```php + 5 +// 1 => 4 +// 2 => 3 +// 3 => 2 +// 4 => 1 +``` + +in_array 检查数组中是否存在某个值 + +```php +in_array(mixed $needle, array $haystack, bool $strict = false): bool + +// eg +$arr = [1, 2, 3, 4, 5]; + +var_dump(in_array(5, $arr)); +// bool(true) + +``` + +array_keys 返回数组中部分的或所有的键名 + +```php +array_keys(array $array): array + +array_keys(array $array, mixed $search_value, bool $strict = false): array + + +// eg +$arr = ['Tom', 'Jack', 'Steve']; + +echo json_encode(array_keys($arr)); +// [0,1,2] +``` + +array_values 返回 input 数组中所有的值并给其建立数字索引。 + +```php +array_values(array $array): array + +// eg +$arr = ['Tom', 'Jack', 'Steve']; + +echo json_encode(array_values($arr)); +// ["Tom","Jack","Steve"] +``` diff --git a/blog/php-basic/index.md b/blog/php-basic/index.md index d1e21e4..5dbf8a5 100644 --- a/blog/php-basic/index.md +++ b/blog/php-basic/index.md @@ -27,3 +27,5 @@ [PHP 数组 array](blog/php-basic/array.md) [PHP 数组相关函数](blog/php-basic/array-function.md) + +[编程思想](blog/php-basic/thinking.md) diff --git a/blog/php-basic/thinking.md b/blog/php-basic/thinking.md new file mode 100644 index 0000000..2118952 --- /dev/null +++ b/blog/php-basic/thinking.md @@ -0,0 +1,81 @@ +# 编程思想 + +编程思想:利用数学模式,来解决对应的需求问题,然后利用代码实现对象的数据模型 + +算法:使用代码实现对应的数学模型,从而解决对应的业务问题 + +## 递推算法 + +是一种简单的算法,即通过已知条件,利用特定关系得出中间结论,直至得到结果的算法 + +递推算法的分类: +- 顺推 通过最简单的条件(已知),然后逐步推演结果 +- 逆推 通过结果找到规律,然后推到已知条件 + +递推思想:菲波那切数列 + +```php +