


There are three functions for merging arrays:
1.array_combine()
carries two parameter arrays. The value of parameter array one is used as the key of the new array, and the value of parameter array two is value as the value of the new array. Very simple.
Example:
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b) ;
print_r($c);
?>
The above example will output:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)
2.array_merge()
carries two parameter arrays, and simply appends array two to the back of array one Create a new array.
Example:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => " trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
3.array_merge_recursive()
is the same as the above function, the only difference is that When appending, it is found that the key to be added already exists. The processing method of array_merge() is to overwrite the previous key value. The processing method of array_merge_recursive() is to reconstruct the sub-array and form a new numerical array with the values of the repeated keys.
Example:
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green ", "blue"));
$result = array_merge_recursive($ar1, $ar2);
?>
The above example will output $result:
Array
(
[color] => Array
(
[ favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
There are two functions for splitting an array:
1.array_slice()
carries three parameters. Parameter one is the target array, parameter two is offset, and parameter three is length. The function is to extract a subarray of length starting from offset from the target array.
If offset is a positive number, the starting position is checked from the beginning of the array. If offset is a negative number, the starting position is checked from the end of the array. If length is a positive number, the number of subarray elements taken out is definitely length. If length is a negative number, the subarray starts from offset and ends at count (target array) - |length| from the beginning of the array. Specifically, if length is empty, the end position is at the end of the array.
Example:
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d" , and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a" , "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input , 2, -1, true));
?>
The above example will output:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
2.array_splice()
carry The three parameters are the same as above, and their function is to delete the subarray with length starting from offset.
Example:
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>
区别取值函数有四个:
1.array_intersect()
携带参数不定,均为数组,返回所有数组中公共元素的值组成的数组,数组的键由所在第一个数组的键给出。
例子:
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>
上例将输出:
Array
(
[a] => green
[0] => red
)
2.array_intersect_assoc()
在前一个函数的基础上,返回所有数组中键、值均相同的键值对。
例子:
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
?>
上例将输出:
Array
(
[a] => green
)
3.array_diff()
携带多个数组,返回第一个数组中有的而后面的数组中没有的所有的值组成的新数组,对应键取自第一个数组。
例子:
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
上例将输出:
Array
(
[1] => blue
)
4.array_diff_assoc()
在前一个函数的基础上,不仅需要匹配值还要匹配键。
例子:
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
?>
上例将输出:
Array
(
[b] => brown
[c] => blue
[0] => red
)

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
