Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:php.net对新人并不友好, 没办法, 技术文档都这样, 想吃碗饭, 就得硬头皮看, 你不想看其它人同样也不想看, 如果你看了,就做了别人做不了事,你就容易成功, 不是吗?
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
// 1. 常规用法, 返回所有键名
$keys = array_keys($arr);
printfpre($keys);
/*
result:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => id
[5] => name
[6] => age
[7] => salary
[8] => in_use
[9] => roles
)
*/
// 2. 只返回键值对的值为某值的键名
/* demo1: 非严格比较, '33' == 33 */
$value = '33';
$keys = array_keys($arr, $value);
printfpre($keys);
/*
result:
Array
(
[0] => age
[1] => in_use
)
*/
/* demo2: 严格比较, '33' !== 33 */
$keys = array_keys($arr, $value, true);
printfpre($keys);
/*
result:
Array
(
)
*/
$value = 33;
$keys = array_keys($arr, $value, true);
printfpre($keys);
/*
Array
(
[0] => age
)
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
$targetKey = 'name';
echobr(array_key_exists($targetKey, $arr) ? '存在' : '不存在');
/*
result: 存在
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
echobr(array_key_first($arr));
/*
result: 0
*/
echobr(array_key_last($arr));
/*
result: roles
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
printfpre(array_values($arr));
/*
result:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 1
[6] => admin
[7] => 33
[8] => 9999
[9] => 1
[10] => Array
(
[0] => 1
[1] => 2
[2] => 34
[3] => 5
[4] => 6
)
)
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
$targetValue = '33';
echobr((in_array($targetValue, $arr) ? '存在' : '不存在'));
/*
默认为不严格比较, '33' == 33
result: 存在
*/
echobr(in_array($targetValue, $arr, true) ? '存在' : '不存在');
/*
使用严格比较, '33' !== 33
result: 不存在
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
$targetValue = '33';
dumpbr(array_search($targetValue, $arr));
/*
不使用严格模式, '33' == 33
result: string(3) "age" */
dumpbr(array_search($targetValue, $arr, true));
/*
严格模式, '33' !== 33, 返回false
result: bool(false)
*/
printfpre(array_unique($arr));
/*
'id'被删除了
result:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[name] => admin
[age] => 33
[salary] => 9999
[roles] => Array
(
[0] => 1
[1] => 2
[2] => 34
[3] => 5
[4] => 6
)
)
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 34, 5, 6],
];
echobr(count($arr));
/*
不包括roles值中数组元素的个数
result: 11
*/
echobr(count($arr, COUNT_RECURSIVE));
/*
包括二维数组(roles值中数组元素的个数)
result: 16
*/
printfpre(array_count_values($arr));
/*
key=原数组值; value=原数组值在原数组中出现的次数; roles的值不能统计
result:
Warning: array_count_values(): Can only count STRING and INTEGER values! in D:\phpstudy_pro\WWW\php11\PHP\0424\homework.php on line 253
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 1
[4] => 1
[admin] => 1
[33] => 1
[9999] => 1
)
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
// 'name' => 'admin',
'age' => '33',
'salary' => 5000,
'in_use' => true,
'roles' => 1, //[1, 2, 34, 5, 6],
'key1' => '100php123',
// 'key2' => 'php100',
];
echobr(array_sum($arr));
/*
会自动跳过非数字值(数字字符串也被视为数字值, true = 1, false = 0), 数组也被忽略, 以数字开头的字符串, 会解析开头的数字部分(100php123=>100)
result: 5145
*/
echobr(array_product($arr));
/*
result: 0 */
$arr = [];
$count = array_push($arr, 1, 2, 3, 4, 5, 6, 7, 8);
echobr('共' . $count . '个元素入栈');
dumpbr($arr);
/*
result:
共8个元素入栈
array(8) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) }
*/
$lastValue = array_pop($arr);
echobr('最后一个元素的值是: ' . $lastValue);
/* result: 最后一个元素的值是: 8 */
$count = array_unshift($arr, 'a', 'b', 'c', 'd');
echobr('共在头部添加' . $count . '个元素');
dumpbr($arr);
/* result:
共在头部添加11个元素
array(11) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> int(1) [5]=> int(2) [6]=> int(3) [7]=> int(4) [8]=> int(5) [9]=> int(6) [10]=> int(7) } */
$delValue = array_shift($arr);
echobr('被删除的数组元素值是: ' . $delValue);
/*
result: 被删除的数组元素值是: a
*/
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'height' => '174cm',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 5, 34, 6],
];
// demo1: 升序, 键名重排
if (sort($arr)) {
echobr('排序成功');
printfpre($arr);
/*
默认排序策略: 字母开头的字符串被解析为0; 数字开头的字符串, 开头的数字部分被解析为数字; boolean值比所有数字大; 数组值比Boolean值大;
注意: 键值已经重排了
result:
Array
(
[0] => admin
[1] => 0
[2] => 1
[3] => 1
[4] => 2
[5] => 3
[6] => 4
[7] => 33
[8] => 174cm
[9] => 9999
[10] => 1
[11] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 34
[4] => 6
)
)
*/
} else {
echobr('排序失败');
}
// demo2: 升序, 键名保持不变
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'height' => '174cm',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 5, 34, 6],
];
if (asort($arr)) {
echobr('排序成功');
printfpre($arr);
/*
result:
Array
(
[name] => admin
[0] => 0
[1] => 1
[id] => 1
[2] => 2
[3] => 3
[4] => 4
[age] => 33
[height] => 174cm
[salary] => 9999
[in_use] => 1
[roles] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 34
[4] => 6
)
) */
} else {
echobr('排序失败');
}
// 排序策略测试略.
// rsort()和arsort()使用方法类似, 略.
// 1. 键名升序
$arr = [
0, 1, 2, 3, 4,
'id' => 1,
'name' => 'admin',
'height' => '174cm',
'age' => 33,
'salary' => 9999,
'in_use' => true,
'roles' => [1, 2, 5, 34, 6],
];
if (ksort($arr)) {
echobr('键名升序排序成功');
printfpre($arr);
/*
默认排序策略的解释, 见6.1-demo1
resunt:
Array
(
[age] => 33
[0] => 0
[height] => 174cm
[id] => 1
[in_use] => 1
[name] => admin
[roles] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 34
[4] => 6
)
[salary] => 9999
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
*/
}
// rksort()使用方法跟ksort()相同, 略.
// 回调函数说明:
function (数组中的值1, 数组中的值2) {
/*在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。*/
}
/* demo1: 模拟升序排列, 键名重排 */
$arr = [
'key1' => 10, 'key2' => 1, 'key3' => 21, 'key4' => 13, 'key5' => 4
];
// 模拟升序排列
usort($arr, function ($val1, $val2) {
return $val1 <=> $val2;
});
printfpre($arr);
/* result: Array
(
[0] => 1
[1] => 4
[2] => 10
[3] => 13
[4] => 21
) */
/* demo2: 模拟降序排列, 键名不重排 */
$arr = [
'key1' => 10, 'key2' => 1, 'key3' => 21, 'key4' => 13, 'key5' => 4
];
// 模拟降序排列, 键名保持不变
uasort($arr, function ($val1, $val2) {
return $val2 <=> $val1;
});
printfpre($arr);
/*
result:
Array
(
[key3] => 21
[key4] => 13
[key1] => 10
[key5] => 4
[key2] => 1
)
*/
// - uksort(待处理数组, 写有排序逻辑的回调函数): 自定义函数对键名进行排序. 略
在字符串中的数字字符串将被当做数字值来排序.
$arr = ['r10c1', 'r1c1', 'r10c3', 'r2c2', 'r1c2', 'r10c2', 'r2c3', 'r2c1', 'r1c3', 'r2c4', 'r1c4', 'r10c4'];
if (natsort($arr)) {
printfpre($arr);
/*
字符串中的'10'将被当做数字10来排序, 而不是字符串从左到右按字符ASCII码排序(sort就是后者)
result:
Array
(
[1] => r1c1
[4] => r1c2
[8] => r1c3
[10] => r1c4
[7] => r2c1
[3] => r2c2
[6] => r2c3
[9] => r2c4
[0] => r10c1
[5] => r10c2
[2] => r10c3
[11] => r10c4
)
*/
}
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
if (shuffle($arr)) {
echobr(implode(', ', $arr));
/* result: 5, 4, 2, 1, 8, 10, 3, 7, 6, 9 */
}
// - array_flip(待处理数组): 交换数组中的键和值, 返回交换后的数组
$arr = [
'key1' => 10, 'key2' => 1, 'key3' => 21, 'key4' => 13, 'key5' => 4
];
$res = array_flip($arr);
printfpre($res);
/*
result:
Array
(
[10] => key1
[1] => key2
[21] => key3
[13] => key4
[4] => key5
)
*/
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/* demo1: 不保留索引数组的键 */
$res = array_reverse($arr);
printfpre($res);
/* result:
Array
(
[0] => 10
[1] => 9
[2] => 8
[3] => 7
[4] => 6
[5] => 5
[6] => 4
[7] => 3
[8] => 2
[9] => 1
)
*/
/* demo2: 保留索引数组的键 */
printfpre(array_reverse([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], true));
/* result:
Array
(
[9] => 10
[8] => 9
[7] => 8
[6] => 7
[5] => 6
[4] => 5
[3] => 4
[2] => 3
[1] => 2
[0] => 1
)
*/
开始截取的索引和截取长度都可以是负数, 截取长度为负数时, 表示从截取的终点索引(从末尾往回数)
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
/* demo1: 从第3个开始截取, 截取4个元素组成数组 */
$res = array_slice($arr, 2, 4);
printfpre($res);
/*
注意: 索引重排了
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
)
*/
/* demo2: 不指定截取长度, 则表示从截取开始位置开始, 截取到数组末尾 */
$res = array_slice($arr, 9);
printfpre($res);
/*
result:
Array
(
[0] => 10
[1] => 11
[2] => 12
)
*/
/* demo3: 保留原索引 */
$res = array_slice($arr, 2, 4, true);
printfpre($res);
/*
数组索引还是元素在原数组中的索引
Array
(
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
*/
/* demo4: 使用负数值1-从倒数第5个元素开始截取, 截取3个元素, 组成新数组 */
$res = array_slice($arr, -5, 3);
printfpre($res);
/*
result:
Array
(
[0] => 8
[1] => 9
[2] => 10
)
*/
/* demo5: 使用负数值2-从倒数第5哥元素开始截取, 截取到倒数第2个, 组成新数组, 保留原数组索引 */
$res = array_slice($arr, -5, -2, true);
printfpre($res);
/*
result:
Array
(
[7] => 8
[8] => 9
[9] => 10
)
*/
/* 可以实现对数组元素的:删除,替换/更新,添加 */
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
/* demo1: 删除数组中第3个开始, 共4个元素 */
$del = array_splice($arr, 2, 4);
echobr('被删除的元素:');
echobr(implode(', ', $del));
echobr('删除元素后的原数组:');
echobr(implode(', ', $arr));
/*
result:
被删除的元素:
3, 4, 5, 6
删除元素后的原数组:
1, 2, 7, 8, 9, 10, 11, 12
*/
/* demo2: 不指定截取长度, 表示删除到数组最后一个元素为止 */
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$del = array_splice($arr, 8);
echobr('被删除的元素:');
echobr(implode(', ', $del));
echobr('删除元素后的原数组:');
echobr(implode(', ', $arr));
/*
result:
被删除的元素:
9, 10, 11, 12
删除元素后的原数组:
1, 2, 3, 4, 5, 6, 7, 8
*/
/* demo3: 可以使用负数值表示从数组倒数为止开始截取--截取后替换成新的数组元素值 */
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$newElements = ['a', 'b', 'c', 'd'];
$del = array_splice($arr, -6, 6, $newElements);
echobr('被替换掉的元素:');
echobr(implode(', ', $del));
echobr('替换元素后的原数组:');
echobr(implode(', ', $arr));
/*
result:
被替换掉的元素:
7, 8, 9, 10, 11, 12
替换元素后的原数组:
1, 2, 3, 4, 5, 6, a, b, c, d
*/
/* demo4: 在第5个元素后面插入新的元素(把截取长度设置为0即可) */
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$newElements = ['a', 'b', 'c', 'd'];
array_splice($arr, 5, 0, $newElements);
echobr('插入新元素后的原数组:');
echobr(implode(', ', $arr));
/*
result:
插入新元素后的原数组:
1, 2, 3, 4, 5, a, b, c, d, 6, 7, 8, 9, 10, 11, 12
*/
/* demo1: 随机从数组中取出一个元素 */
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$res = array_rand($arr);
echobr('取出的元素:');
echobr($res);
/* demo2: 随机从数组中取出多个值 */
$res = array_rand($arr, 3);
echobr('取出的元素:');
echobr(implode(', ', $res));
/*
result:
取出的元素:
2, 7, 10
*/
说明: 入参2和入参3, 如果待处理的数组是索引数组, 则其值为索引值; 如果待处理数组是关联数组, 则为键名
$arr = [
['id' => 1, 'name' => '张三', 'sex' => 'male', 'age' => 25, 'salary' => 9999],
['id' => 2, 'name' => '李四', 'sex' => 'female', 'age' => 30, 'salary' => 8888],
['id' => 3, 'name' => '王五', 'sex' => 'male', 'age' => 35, 'salary' => 7777],
['id' => 4, 'name' => '赵六', 'sex' => 'male', 'age' => 40, 'salary' => 11111],
['id' => 5, 'name' => '钱七', 'sex' => 'female', 'age' => 45, 'salary' => 9797],
['id' => 6, 'name' => '吴八', 'sex' => 'male', 'age' => 28, 'salary' => 7979],
['id' => 7, 'name' => '陈九', 'sex' => 'male', 'age' => 37, 'salary' => 8866],
];
/* demo1: 取出月薪列的内容, 以name列值作为取出内容的键名 */
$res = array_column($arr, 'salary', 'name');
printfpre($res);
/*
result:
Array
(
[张三] => 9999
[李四] => 8888
[王五] => 7777
[赵六] => 11111
[钱七] => 9797
[吴八] => 7979
[陈九] => 8866
)
*/
/* demo2: 取出第4列值, 以第2列为键名(索引数组才有效) */
// $res = array_column($arr, 3, 1);
// printfpre($res);
/* demo3: 取出年龄列内容 */
$res = array_column($arr, 'age');
printfpre($res);
/*
result:
Array
(
[0] => 25
[1] => 30
[2] => 35
[3] => 40
[4] => 45
[5] => 28
[6] => 37
)
*/
替换规则: 用替换数组中的值, 替换原数组中相同键名的元素的值. 当有多个替换数组, 且这些数组中有相同的键名元素时, 后面数组元素值的会覆盖前面的数组元素值
/* demo: */
$arr = ['one', 'two', 'three', 'four'];
$cover1 = [0 => '1', 4 => '5th'];
$cover2 = [0 => 'first'];
$res = array_replace($arr, $cover1, $cover2);
echobr(implode(', ', $res));
/*
result: first, two, three, four, 5th
*/
// array_replace_recursive() 太复杂了, 略.
即, 查找出在所有数组中都出现的键值对(值相等就算, 若值相等键名不等, 则以第一个数组的键名为键名)
$arr = ['first' => '1st', 'second' => '2nd', 'third' => '3rd', 'fourth' => '4th', 'fifth' => '5th'];
$intersect1 = ['first' => '1st', 'second' => '第二', 'seventh' => '5th'];
$intersect2 = ['first' => '1st', 'second' => '2nd', 'fourth' => '5th'];
$res = array_intersect($arr, $intersect1, $intersect2);
printfpre($res);
/*
result:
Array
(
[first] => 1st
[fifth] => 5th
)
*/
跟array_interrect()用法一样, 只是array_intersect_assoc()不光要值相等, 键名也要相等才算
$res = array_intersect_assoc($arr, $intersect1, $intersect2);
printfpre($res);
/*
5th值的键名不相等, 所以5th并没有在交集中
result:
Array
(
[first] => 1st
)
*/
$arr = ['first' => '1st', 'second' => '2nd', 'third' => '3rd', 'fourth' => '4th', 'fifth' => '5th'];
$compare = ['first' => '1st', 'second' => '第二', 'seventh' => '5th'];
$res = array_diff($arr, $compare);
printfpre($res);
/*
result:
Array
(
[second] => 2nd
[third] => 3rd
[fourth] => 4th
)
*/
函数名 | 函数功能 |
---|---|
current | 获取当前元素的值 |
key | 获取当前元素的键名 |
next | 将数组中的内部指针向前移动一位 |
prev | 将数组内部指针倒回一位 |
end | 讲数组的内部指针指向最后一个单元 |
reset | 讲数组的内部指针指向第一个单元 |
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// 获取当前数组指针指向的键名和值
echobr('[' . key($arr) . '] => ' . current($arr));
/* result: [0] => 1 */
// 数组指针向后移动3位(即, 指向第四个元素)
next($arr);
next($arr);
next($arr);
echobr('[' . key($arr) . '] => ' . current($arr));
/* result: [3] => 4 */
// 数组指针往回移一位(即, 指向第三个元素)
prev($arr);
echobr('[' . key($arr) . '] => ' . current($arr));
/* result: [2] => 3 */
// 数组指针移到最有一个元素
end($arr);
echobr('[' . key($arr) . '] => ' . current($arr));
/* result: [8] => 9 */
// 数组指针重置(指回第一个元素)
reset($arr);
echobr('[' . key($arr) . '] => ' . current($arr));
/* result: [0] => 1 */
$keys = ['name', 'age', 'sex', 'salary'];
$values = ['zhangsan', 32, 'male', 8888];
$res = array_combine($keys, $values);
printfpre($res);
/*
result:
Array
(
[name] => zhangsan
[age] => 32
[sex] => male
[salary] => 8888
)
*/
/* demo1: 元素键名不全是索引的数组*/
$arr = ['host' => 'localhost', 'username' => 'root', 'password' => 'root', 'port' => 3306, 'dbname' => 'phpedu', 0 => 'hello'];
$forCombine = ['host' => '127.0.0.1', 'password' => '123456', 0 => 'hi~'];
$res = array_merge($arr, $forCombine);
printfpre($res);
/*
注意: 索引值键名并没有被合并, 而是追加到合并数组中(0 => 'hi~')
result:
Array
(
[host] => 127.0.0.1
[username] => root
[password] => 123456
[port] => 3306
[dbname] => phpedu
[0] => hello
[1] => hi~
)
*/
/* demo2: 索引数组 */
$arr = [1, 2, 8 => 3, 9 => 4, 10 => 11, 11 => 20];
$res = array_merge($arr);
printfpre($res);
/*
数组键名被重排
result:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 11
[5] => 20
)
*/
$arr = [
['id' => 1, 'name' => '张三', 'sex' => 'male', 'age' => 25, 'salary' => 9999],
['id' => 2, 'name' => '李四', 'sex' => 'female', 'age' => 30, 'salary' => 8888],
['id' => 3, 'name' => '王五', 'sex' => 'male', 'age' => 35, 'salary' => 7777],
['id' => 4, 'name' => '赵六', 'sex' => 'male', 'age' => 40, 'salary' => 11111],
['id' => 5, 'name' => '钱七', 'sex' => 'female', 'age' => 45, 'salary' => 9797],
['id' => 6, 'name' => '吴八', 'sex' => 'male', 'age' => 28, 'salary' => 7979],
['id' => 7, 'name' => '陈九', 'sex' => 'male', 'age' => 37, 'salary' => 8866],
];
/* demo: 模拟分页, 第三个参数默认为false */
$pageSize = 2;
$data = array_chunk($arr, $pageSize);
printfpre($data);
/* result: 返回结果是以分割成的数组为元素值的索引数组. 返回结果太长, 结果略.
*/
可以是数字, 字母
/* demo1: 从1开始, 每隔3个数, 取一个数值, 直到20 */
$arr = range(1, 20, 3);
echobr(implode(', ', $arr));
/* result: 1, 4, 7, 10, 13, 16, 19 */
/* demo2: 从'h'开始, 每隔4个字母, 取一个字母, 直到'z' */
$arr = range('h', 'z', 4);
echobr(implode(', ', $arr));
/* result: h, l, p, t, x */
/* demo1: 索引从5开始, 填充5个元素, 填充值为100 */
$arr = array_fill(5, 5, 100);
printfpre($arr);
/* result:
Array
(
[5] => 100
[6] => 100
[7] => 100
[8] => 100
[9] => 100
)
*/
/* demo */
$res = array_fill_keys(['key1', 'key2', 'key3', 'key4'], 100);
printfpre($res);
/*
result:
Array
(
[key1] => 100
[key2] => 100
[key3] => 100
[key4] => 100
)
*/
$arr = [1, 2, 3, 4, 5];
/* demo1: 向右填充 */
$res = array_pad($arr, 10, 100);
echobr(implode(', ', $res));
/* result: 1, 2, 3, 4, 5, 100, 100, 100, 100, 100 */
/* demo1: 向左填充(田崇厚的数组元素总数为负数) */
$res = array_pad($arr, -10, 100);
printfpre($res);
/*
填充完成后的键名会重排
result:
Array
(
[0] => 100
[1] => 100
[2] => 100
[3] => 100
[4] => 100
[5] => 1
[6] => 2
[7] => 3
[8] => 4
[9] => 5
)
*/
/* demo1: */
$userInfo = ['zhangsan', '张三', '男', 34]; //['username' => 'zhangsan', 'realname' => '张三', 'sex' => '男', 'age' => 34];
list($username, $realname, $sex, $age) = $userInfo;
echobr("用户名: {$username}, 姓名: {$realname}, 性别: {$sex}, 年龄: {$age}");
/*
result:
用户名: zhangsan, 姓名: 张三, 性别: 男, 年龄: 34
*/
/* demo2: PHP7.1+ 支持关联数组 */
$arr = [
['id' => 1, 'name' => '张三', 'sex' => 'male', 'age' => 25, 'salary' => 9999],
['id' => 2, 'name' => '李四', 'sex' => 'female', 'age' => 30, 'salary' => 8888],
['id' => 3, 'name' => '王五', 'sex' => 'male', 'age' => 35, 'salary' => 7777],
['id' => 4, 'name' => '赵六', 'sex' => 'male', 'age' => 40, 'salary' => 11111],
['id' => 5, 'name' => '钱七', 'sex' => 'female', 'age' => 45, 'salary' => 9797],
['id' => 6, 'name' => '吴八', 'sex' => 'male', 'age' => 28, 'salary' => 7979],
['id' => 7, 'name' => '陈九', 'sex' => 'male', 'age' => 37, 'salary' => 8866],
];
foreach ($arr as ['id' => $id, 'name' => $name, 'salary' => $salary]) {
echobr("id: {$id}, name: {$name}, salary: {$salary}");
}
/*
result:
id: 1, name: 张三, salary: 9999
id: 2, name: 李四, salary: 8888
id: 3, name: 王五, salary: 7777
id: 4, name: 赵六, salary: 11111
id: 5, name: 钱七, salary: 9797
id: 6, name: 吴八, salary: 7979
id: 7, name: 陈九, salary: 8866
*/
/* demo3: PHP7.1+ 支持关联数组 */
$userInfo = ['username' => 'zhangsan', 'realname' => '张三', 'sex' => '男', 'age' => 34];
list('username' => $username, 'age' => $age) = $userInfo;
echobr("姓名: {$username}, 年龄: {$age}");
/* result: 姓名: zhangsan, 年龄: 34 */
/* demo4: 索引数组中只取出其中几个值 */
$userInfo = ['zhangsan', '张三', '男', 34];
list($username,,, $age) = $userInfo;
echobr("姓名: {$username}, 年龄: {$age}");
/* result: 姓名: zhangsan, 年龄: 34 */
// 另一种写法
list(0 => $username, 3 => $age) = $userInfo;
echobr("姓名: {$username}, 年龄: {$age}");
/* result: 姓名: zhangsan, 年龄: 34 */
$arr = [1, 2, 3, 4, 5, 6];
$arrStr = implode(', ', $arr);
echobr($arrStr);
/* result: 1, 2, 3, 4, 5, 6 */
$arrStr = '1, 2, 3, 4, 5, 6';
$arr = explode(', ', $arrStr);
dumpbr($arr);
/* result: array(6) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" [5]=> string(1) "6" } */
$userInfo = ['username1' => 'zhangsan', 'realname1' => '张三', 'sex1' => '男', 'age1' => 34];
extract($userInfo);
echobr("用户名: {$username1}, 姓名: {$realname1}, 性别: {$sex1}, 年龄: {$age1}");
/* result: 用户名: zhangsan, 姓名: 张三, 性别: 男, 年龄: 34 */
/* demo1: */
$username = 'lisi';
$realname = '李四';
$sex = '女';
$age = '23';
$arr = compact('username', 'realname', 'sex', 'age');
printfpre($arr);
/* result:
Array
(
[username] => lisi
[realname] => 李四
[sex] => 女
[age] => 23
)
*/
/* demo2: 也可以把全部或部分变量名以数组的形式传入 */
$keys = ['username', 'realname', 'sex', 'age'];
$arr1 = compact($keys);
printfpre($arr1);
/*
result:
Array
(
[username] => lisi
[realname] => 李四
[sex] => 女
[age] => 23
)
*/
// 创建一个1-20的数组
$arr = range(1, 20);
/* demo1: 过滤出所有的偶数元素 */
$res = array_filter($arr, function ($val) {
if ($val % 2 == 0) {
return true;
}
return false;
});
// 使用array_merge()函数重排键名
$res = array_merge($res);
printfpre($res);
/* demo1: 遍历数组, 1000以下不打折; 1000-3000打9折; 3001-5000打8折; 5001以上打7折 */
$prices = [888, 1024, 3099, 4799, 5555, 9999];
$cutPrices = array_map(function ($price) {
switch ($price) {
case ($price >= 1000 && $price <= 3000):
return $price * 0.9;
break;
case ($price > 3000 && $price <= 5000):
return $price * 0.8;
break;
case ($price > 5000):
return $price * 0.7;
break;
default:
return $price;
}
}, $prices);
printfpre($cutPrices);
/*
result:
Array
(
[0] => 888
[1] => 921.6
[2] => 2479.2
[3] => 3839.2
[4] => 3888.5
[5] => 6999.3
)
*/
/* demo2: 有多个数组的情况-计算面积 */
$length = [3, 5, 10];
$width = [7, 2, 4];
$area = array_map(function ($l, $w) {
return $l * $w;
}, $length, $width);
printfpre($area);
/*
result:
Array
(
[0] => 21
[1] => 10
[2] => 40
)
*/
$arr = [1, 2, 3, 4, 5];
/* demo1: 所有数组元素乘以x */
array_walk($arr, function ($val, $key, $userdata) {
echobr("每个元素值将会乘以: {$userdata}");
$key += 1;
$res = $val * $userdata;
echobr("处理第{$key}个元素: {$val} 乘以 {$userdata} 等于 {$res}");
echobr();
}, 10);
/* result:
每个元素值将会乘以: 10
处理第1个元素: 1 乘以 10 等于 10
每个元素值将会乘以: 10
处理第2个元素: 2 乘以 10 等于 20
每个元素值将会乘以: 10
处理第3个元素: 3 乘以 10 等于 30
每个元素值将会乘以: 10
处理第4个元素: 4 乘以 10 等于 40
每个元素值将会乘以: 10
处理第5个元素: 5 乘以 10 等于 50
*/