潘逸安的博客

潘逸安

PHP二维数组排列组合

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
function combination(array $options)
{
$rows = [];

foreach ($options as $option => $items) {
if (count($rows) > 0) {
// 2、将第一列作为模板
$clone = $rows;

// 3、置空当前列表,因为只有第一列的数据,组合是不完整的
$rows = [];

// 4、遍历当前列,追加到模板中,使模板中的组合变得完整
foreach ($items as $item) {
$tmp = $clone;
foreach ($tmp as $index => $value) {
$value[$option] = $item;
$tmp[$index] = $value;
}

// 5、将完整的组合拼回原列表中
$rows = array_merge($rows, $tmp);
}
} else {
// 1、先计算出第一列
foreach ($items as $item) {
$rows[][$option] = $item;
}
}
}

return $rows;
}

$options = array(
'sex' => [1, 2],
'area' => [1, 2, 3, 4, 5, 6, 7, 8, 9],
'level' => [1, 2, 3, 4],
);
$rows = combination($options);

PHP中 guzzle 如何使用cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$jar = new \GuzzleHttp\Cookie\CookieJar();
$domain = 'www.hz419.com';
$cookies = [
'JSESSIONID' => '89886942104CF3E80D2E4B7AEA556445',
//
];
$cookieJar = $jar->fromArray($cookies, $domain);
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'http://www.hz419.com',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('GET', '/blackweb/quoteList.action', ['cookies' => $cookieJar]);

Mac os 重置密码

进入 OS X 恢复模式重置用户密码

  • 关闭你的 Mac。
  • 按住 Command + R(⌘R) 组合键,并点按开机按钮,直到出现 转载自斗玩网 标志,进入恢复模式(Recovery Mode)(当然,你也可以先按开机键,在听到启动声后,立即按住 ⌘R 键)。
  • 选择「以简体中文作为主要语言」(或其他语言),点击向右的箭头。
  • 在「实用工具」菜单栏中选择「终端」。
  • 在终端中输入命令(注:连写,且均为小写字母):resetpassword,回车确认。
  • 在出现的「重设密码」窗口中,依次选择包含密码的启动磁盘卷宗、希望重设的用户账户;
  • 输入并确认新的用户密码,并为其设置密码提示信息(可选);
  • 点击「重设」。点击菜单栏中的 ,并选择「重启」或「关机」。下次启动时,使用新密码登录即可。

Sequel pro

Sequel-Pro Keyboard Shortcuts Sequel-Pro键盘快捷键


Get the most out of Sequel Pro by remembering the following keyboard shortcuts. 通过记住以下键盘快捷键来最有效地使用Sequel Pro。

Main Tabs 主要标签


Table Structure 表结构

⌘   1

Table Content 表内容

⌘   2

Table Relations 表关系

⌘   3

Table Info 表信息

⌘   4

Custom Query 自定义查询

⌘   5

Table Triggers 表触发器

⌘   6

General Shortcuts 常用快捷键


New Window (Connection File) 新窗口(连接文件)

⌘   N

New Tab(Connection File) 新标签(连接文件)

⌘   T

Add Connection To Favorites 添加连接到收藏夹

⇧   ⌘   A

Open (Connection File or SQL File) 打开(连接文件或者SQL文件)

⌘   O

Open current Connection File in New Window在新窗口打开当前连接

⌥   ⌘   O

Save (Connection File)保存(连接文件)

⌘   S

Save As (Connection File) 另存为(连接文件)

⇧  ⌘   S

Save Query保存查询

^  ⌘   S

Close (Connection File)关闭(连接)

⌘   W

Close All (Connection File) 关闭所有(连接)

⌥   ⌘   W

Print打印

⌘   P

Import 导入

⇧   ⌘   I 

Show Console Window 显示控制台

⇧   ⌘   K

Clear Console 清空控制台

⌘   K 

Back in History 后退

⌃   ⌥   ←

Forward in History 向前

⌃   ⌥   →

Select Next Tab 选择下一个标签

⌃   ⇥

Select Previous Tab 选择上一个标签

⌃   ⇧   ⇥ 

Insert NULL value 插入NULL

⌃   ⇧   N

Copy Create Table Syntax 复制创建表语法

⇧   ⌘   C

Show Create Table Syntax 显示创建表语法

⌥   ⌘   S

Refresh Databases 刷新数据库

⇧   ⌘   R

Choose Database 选择数据库

⇧   ⌘   D

Show Server Variables 显示服务器变量

⇧   ⌘   V

Show Server Processes 显示服务器进程

⌥   ⌘   P

Refresh Tables 刷新数据表

⌃   ⌘   R

Flush Privileges 刷新权限

⇧   ⌘   F

User Accounts… 用户账号

⌘   U

Copy selection / Copy selected row(s) 复制选择/复制选中的行

⌘   C

Copy selected row(s) with column names 复制选中的行包括列名

⌥   ⌘   C

Copy selected row(s) as SQL INSERT 复制选中的行包括插入语句

⌃   ⌥   ⌘   C

Show/Hide Toolbar 显示/隐藏工具栏

⇧   ⌘   T

Filter Table Content 过滤表内容

⌃   ⌘   F

Filter Tables 过滤表

⌃   ⌥   ⌘   F

Bundle Editor 构建编辑器

⌃   ⌥   ⌘   B

Navigator 导航

⌃   ⌥   ⌘   N

Select 选择


Select current word 选择当前单词

⌃   W 

Select current line 选择当前行

⌃   L

Select enclosing brackets 选择括号内

⇧   ⌘   B

Select all 选择所有

⌘   A

Table Structure Tab 表结构标签


Add a new field 新增字段

⌥   ⌘   A

Delete selected field 删除所选字段

Duplicate selected field 选中字段的副本

⌘   D

Refresh table structure 刷新表结构

⌘   R

Edit table details 编辑表详情

⌘   4

Table Content Tab 表内容标签


Add a new row 新增行

⌥   ⌘   A

Delete selected row(s) 删除选中行

Duplicate selected row 选中行的副本

⌘   D

Refresh table contents 刷新表内容

⌘   R

Custom Query Tab 自定义查询标签


Version 1.0 and later 1.0版本以后

Run all queries 运行所有查询

⌥   ⌘   R

Run current query or selection 运行当前或者选中查询

⌅ or ⌘   R

Versions 0.9.9.1 and earlier 1.0版本以前

Run all queries 运行所有查询

⌅ or ⌘   R

Run current query or selection 运行当前或者选中查询

⌥   ⌘   R

Shift line or selection rightwards 左缩进

⌘   [

Shift line or selection leftwards 右缩进

⌘   ]

(Un)Comment line or selection 注释选中行

⌘   /

(Un)Comment current query 注释当前查询

⌥   ⌘   /

Completion (narrow-down list / fuzzy search) 完成(模糊查询)

    Insertion of an item 插入项目

↩ , ⇥ , Double-Click

    Insertion full schema reference

⇧ + ↩ , ⇧   ⇥ , ⇧   Double-Click

    Keep Window Open after Insertion 保存窗口打开后插入

holding down ⌃while insertion

    Fuzzy Search 模糊查询

⌃   ⎋

    Close Window 关闭窗口

Spell Checker Completion (narrow-down list) 拼写检查

F5

Select current query 选择当前查询

⌃   Y

Show MySQL help for current word or selection 显示当前单词或者选择的mysql帮助

⌃   H

Open query favorites popup menu 打开查询收藏夹弹窗

⌥   ⌘   F

Open query history popup menu 打开查询纪录弹窗

⌥   ⌘   Y

Insert previous history item (successively) 插入上一个语句(连续的)

⌃ +  ↑

Insert next history item (successively) 插入下一个语句(连续的)

⌃   ↓

Show all database names as completion list 显示所有数据库名到补齐列表

⌥   ⌘   1

Show all table and view names as completion list 显示所有表名到补齐列表

⌥   ⌘   2

Show all table names from current database as completion list 显示当前数据库的所有表名到补齐列表

⌥   ⌘   3

Laravel 辅助函数

  • TOC
    {:toc}

新增

array_add($array, $field, $value) 不覆盖|不改变原数组

如果给定的键不在数组中,那么 array_add 函数将会把给定的键/值对添加到数组中:

1
2
3
$array = array_add(['name' => 'Desk'], 'price', 100);

// ['name' => 'Desk', 'price' => 100]

array_prepend($array, $value, $key =null) 可覆盖|不改变原数组

array_prepend 函数将一个项目推到数组的开头:

1
2
3
4
5
$array = ['one', 'two', 'three', 'four'];

$array = array_prepend($array, 'zero');

// ['zero', 'one', 'two', 'three', 'four']

你可以指定用于该值的键:

1
2
3
4
5
$array = ['price' => 100];

$array = array_prepend($array, 'Desk', 'name');

// ['name' => 'Desk', 'price' => 100]

array_set($array, 'first.second', $value) 覆盖|改变原数组|返回当前深度数组

array_set 函数使用「点」符号在深度嵌套的数组中设置一个值:

1
2
3
4
5
$array = ['products' => ['desk' => ['price' => 100]]];

array_set($array, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

data_fill($target, 'frist.second', $value) 不覆盖|改变原结构|返回target

data_fill 函数使用「点」符号在嵌套数组或对象内设置缺少的值:

1
2
3
4
5
6
7
8
9
$data = ['products' => ['desk' => ['price' => 100]]];

data_fill($data, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 100]]]

data_fill($data, 'products.desk.discount', 10);

// ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]

该函数也接受星号「 * 」作为通配符,并相应地填写目标:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$data = [
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2'],
],
];

data_fill($data, 'products.*.price', 200);

/*
[
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2', 'price' => 200],
],
]
*/

data_set($target, 'first.second') 嵌套设置|覆盖|改变原结构|返回$target

data_set 函数使用「点」符号在嵌套数组或对象内设置一个值:

1
2
3
4
5
$data = ['products' => ['desk' => ['price' => 100]]];

data_set($data, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

这个函数也接受通配符「 * 」,并相应地在目标上设置值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$data = [
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2', 'price' => 150],
],
];

data_set($data, 'products.*.price', 200);

/*
[
'products' => [
['name' => 'Desk 1', 'price' => 200],
['name' => 'Desk 2', 'price' => 200],
],
]
*/

默认情况下,所有现有的值都会被覆盖。如果你只想设置一个不存在值,你可以传递 false 作为第三个参数:

1
2
3
4
5
$data = ['products' => ['desk' => ['price' => 100]]];

data_set($data, 'products.desk.price', 200, false);

// ['products' => ['desk' => ['price' => 100]]]

删除

array_except($array, array $except) 排除键/键值对|不改变原数组|返回新数组

array_except 函数从数组中删除给定的键/值对:

1
2
3
$array = ['name' => 'Desk', 'price' => 100];
$filtered = array_except($array, ['price']);
// ['name' => 'Desk']

array_forget($array, 'first.second') 多维数组中移除键/值对|改变原数组|返回null

array_forget 函数使用「点」符号从深度嵌套数组中移除给定的键/值对:

1
2
3
4
5
$array = ['products' => ['desk' => ['price' => 100]]];

array_forget($array, 'products.desk');

// ['products' => []]

array_pull($array, 'key') 删除数组键值对,返回值,改变数组

array_pull 函数返回并从数组中删除键/值对:

1
2
3
4
5
6
7
$array = ['name' => 'Desk', 'price' => 100];

$name = array_pull($array, 'name');

// $name: Desk

// $array: ['price' => 100]

将默认值作为第三个参数传递给该方法。如果键不存在,则返回该值:

1
$value = array_pull($array, $key, $default);

检索

array_first($array, $callback, $default) 返回第一个

array_first 函数返回数组中第一个通过指定测试的元素:

1
2
3
4
5
6
7
$array = [100, 200, 300];

$first = array_first($array, function ($value, $key) {
return $value >= 150;
});

// 200

将默认值作为第三个参数传递给该方法。如果没有值通过测试,则返回该值:

1
$first = array_first($array, $callback, $default);

array_get($array, 'first.second') 多维数组中获取值

array_get 函数使用「点」符号从深度嵌套的数组中检索值:

1
2
3
4
5
$array = ['products' => ['desk' => ['price' => 100]]];

$price = array_get($array, 'products.desk.price');

// 100

array_get 函数也接受一个默认值,如果没有找到指定的健,则返回该值:

1
2
3
$discount = array_get($array, 'products.desk.discount', 0);

// 0

array_has($array, 'first.second'|$array) 多维数组中判断存在值

array_has 函数使用「点」符号检查数组中是否存在给定的项目或项目组:

1
2
3
4
5
6
7
8
9
$array = ['product' => ['name' => 'Desk', 'price' => 100]];

$contains = array_has($array, 'product.name');

// true

$contains = array_has($array, ['product.price', 'product.discount']);

// false

array_last($array, $callback, $default)最后一个

array_last 函数返回数组中最后一个通过指定测试的元素:

1
2
3
4
5
6
7
$array = [100, 200, 300, 110];

$last = array_last($array, function ($value, $key) {
return $value >= 150;
});

// 300

将默认值作为第三个参数传递给该方法。如果没有值通过测试,则返回该值:

1
$last = array_last($array, $callback, $default);

array_only($array, $fields) 仅返回给定数组中指定的键/值对

array_only 函数仅返回给定数组中指定的键/值对:

1
2
3
4
5
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$slice = array_only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]

array_pluck($array, 'developer.name') 数组中返回检索给定键的值

array_pluck 函数从数组中检索给定键的所有值:

1
2
3
4
5
6
7
8
$array = [
['developer' => ['id' => 1, 'name' => 'Taylor']],
['developer' => ['id' => 2, 'name' => 'Abigail']],
];

$names = array_pluck($array, 'developer.name');

// ['Taylor', 'Abigail']

你也可以指定生成的列表的键:

1
2
3
$names = array_pluck($array, 'developer.name', 'developer.id');

// [1 => 'Taylor', 2 => 'Abigail']

array_random($array, $limit = 1) 返回随机值

array_random 函数从数组中返回一个随机值:

1
2
3
4
5
$array = [1, 2, 3, 4, 5];

$random = array_random($array);

// 4 - (retrieved randomly)

你也可以指定要返回的随机数的数量作为第二个可选参数。一旦你指定了第二个参数,即使数量为 1,这个函数也会返回一个数组:

1
2
3
$items = array_random($array, 2);

// [2, 5] - (retrieved randomly)

data_get($target, 'first.second') 嵌套获取值

data_get 函数使用「点」符号从嵌套数组或对象中检索值:

1
2
3
4
5
$data = ['products' => ['desk' => ['price' => 100]]];

$price = data_get($data, 'products.desk.price');

// 100

data_get 函数还接受默认值作为第三个参数,如果找不到指定的键,将返回该值:

1
2
3
$discount = data_get($data, 'products.desk.discount', 0);

// 0

head($array) 第一个(不过滤)

head 函数返回给定数组中的第一个元素:

1
2
3
4
5
$array = [100, 200, 300];

$first = head($array);

// 100

last($array) 最后一个(不过滤)

last 函数返回给定数组中的最后一个元素:

1
2
3
4
5
$array = [100, 200, 300];

$last = last($array);

// 300

数组间操作

array_collapse(...$arrays) 合并数组

array_collapse 函数将多个单数组合并成一个数组:

1
2
3
$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

array_divide($array) 分割数组为 键值 两个数组

array_divide 函数返回两个数组,一个包含原始数组的健,另一个包含原始数组的值:

1
2
3
4
5
[$keys, $values] = array_divide(['name' => 'Desk']);

// $keys: ['name']

// $values: ['Desk']

array_dot($array) 平铺多维数组为关联数组,「点」连接

array_dot 函数将多维数组平铺到一维数组中,该数组使用「点」符号表示深度:

1
2
3
4
$array = ['products' => ['desk' => ['price' => 100]]];

$flattened = array_dot($array);
// ['products.desk.price' => 100]

array_flatten($array) 平铺多维数组为索引数组

array_flatten 函数将多维数组平铺为一维数组。

1
2
3
4
5
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];

$flattened = array_flatten($array);

// ['Joe', 'PHP', 'Ruby']

array_sort($array, $callback) 排序

array_sort 函数按照其值排序数组:

1
2
3
4
5
$array = ['Desk', 'Table', 'Chair'];

$sorted = array_sort($array);

// ['Chair', 'Desk', 'Table']

你也可以按给定的闭包返回的结果对数组进行排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$array = [
['name' => 'Desk'],
['name' => 'Table'],
['name' => 'Chair'],
];

$sorted = array_values(array_sort($array, function ($value) {
return $value['name'];
}));

/*
[
['name' => 'Chair'],
['name' => 'Desk'],
['name' => 'Table'],
]
*/

array_sort_recursive($array, $callback) 递归排序

array_sort_recursive 函数使用 sort 函数递归排序数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
$array = [
['Roman', 'Taylor', 'Li'],
['PHP', 'Ruby', 'JavaScript'],
];

$sorted = array_sort_recursive($array);

/*
[
['Li', 'Roman', 'Taylor'],
['JavaScript', 'PHP', 'Ruby'],
]
*/

array_where($array, $callback) 过滤数组

array_where 函数使用给定的闭包来过滤数组:

1
2
3
4
5
6
7
$array = [100, '200', 300, '400', 500];

$filtered = array_where($array, function ($value, $key) {
return is_string($value);
});

// [1 => 200, 3 => 400]

array_wrap() 创建数组

array_wrap 函数将给定的值包装成一个数组。如果给定的值已经是一个数组,则不会被改变:

1
2
3
4
5
$string = 'Laravel';

$array = array_wrap($string);

// ['Laravel']

如果给定的值是空,则返回一个空数组:

1
2
3
4
5
$nothing = null;

$array = array_wrap($nothing);

// []

CentOS 系统 安装与升级ruby方法

  • TOC
    {:toc}

    背景

    在做redis集群时,所需要的使用ruby工具进行操作,发现在线安装的Ruby版本过低,redis支持的版本最少为2.2.2.

 

在线安装ruby

使用yum在线安装ruby,安装的版本为2.0.0。

1
yum install ruby
1
ruby -v

添加ruby仓库

添加aliyun镜像并检测Ruby版本

1
2
3
gem sources -a http://mirrors.aliyun.com/rubygems/

ruby -v

安装RAM

RAM(Ruby Version Manager )是一款RAM的命令行工具,可以使用RAM轻松安装,管理Ruby版本。RVM包含了Ruby的版本管理和Gem库管理(gemset)

可以使用如下命令进行安装RAM:

1
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

2019-01-30 2.27.58.png{: .no-resize.center}

1
curl -sSL https://get.rvm.io | bash -s stable

出现如下信息代表安装成功: 

更新配置文件,使其立马生效:

1
source /etc/profile.d/rvm.sh

查看RVM版本信息,如果可以代表安装成功。

1
rvm -v

接下来查看Ruby版本:

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
$rvm list known

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.0.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.100]
rbx-head

# TruffleRuby
truffleruby[-1.0.0-rc2]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.0]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]

安装Ruby,从上面查到的信息随便找一个比2.2.2版本要高的就行:

1
rvm install 2.5

出现如下信息,代表安装成功: 

验证版本:
成功升级。
 注意:
如果使用rvm安装发现下载缓慢,可以考虑删除原来的仓库地址,只保留阿里云镜像。

1
gem sources --remove https://rubygems.org/