PHP中的数组(拆分与合并)

时间:2022-09-03 22:04:44

explode

使用一个字符串分割另一个字符串

$str = "11,22,33";
$arr = explode(",",$str);
print_r($arr);

Array
(
[0] => 11
[1] => 22
[2] => 33
)

implode

将一个一维数组的值转化为字符串

$arr = [11,22,33];
$str = implode("#",$arr);
echo $str;

11#22#33

split

用正则表达式将字符串分割到数组中

preg_split()函数使用了Perl兼容正则表达式语法,通常是比split()更快的替代方案。如果不需要正则表达式的威力,则使用explode()更快,这样就不会招致正则表达式引擎的浪费。

该函数在PHP7中已被废弃

$str = "123dqaw76eqwv8902fwer12356";
$arr = split("[a-z]+", $str);
print_r($arr);

Array
(
[0] => 123
[1] => 76
[2] => 8902
[3] => 12356
)

preg_split

通过一个正则表达式分隔字符串

如果你不需要正则表达式功能,可以有更快(并且更简单)的选择比如 explode() 或 str_split()。

如果没有成功匹配,将会返回一个数组,包含了单个元素,即输入的字符串。

$str = "123dqaw76eqwv8902fwer12356";
$arr = preg_split("/[a-z]+/i", $str);
print_r($arr);

Array
(
[0] => 123
[1] => 76
[2] => 8902
[3] => 12356
)

str_split

将字符串转换为数组

$str = "hello world";
print_r(str_split($str));
print_r(str_split($str, 3));

Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => w
[7] => o
[8] => r
[9] => l
[10] => d
)
Array
(
[0] => hel
[1] => lo
[2] => wor
[3] => ld
)

array_chunk

将一个数组分割成多个

$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));

Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
[1] => d
)

[2] => Array
(
[0] => e
)

)
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[2] => c
[3] => d
)

[2] => Array
(
[4] => e
)

)

array_combine

创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);

Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)

array_merge

合并一个或多个数组。一个数组中的值附加在前一个数组的后面,返回作为结果的数组。

别忘了数字键名将会被重新编号!

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

Array
(
[color] => red
[0] => 2
[1] => 4
)
Array
(
[0] => a
[1] => b
[color] => green
[shape] => trapezoid
[2] => 4
)
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)

如果你想完全保留原有数组并只想新的数组附加到后面,用 + 运算符。如果两个被合并的数组含有相同的key,则保留第一个,忽略后边的。

$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
print_r($result);

Array
(
[0] => zero_a
[2] => two_a
[3] => three_a
[1] => one_b
[4] => four_b
)

array_merge_recursive

递归地合并一个或多个数组

如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。

$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($ar1);
print_r($ar2);
print_r($result);

Array
(
[color] => Array
(
[favorite] => red
)

[0] => 5
)
Array
(
[0] => 10
[color] => Array
(
[favorite] => green
[0] => blue
)

)
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)

[0] => blue
)

[0] => 5
[1] => 10
)

compact

建立一个数组,包括变量名和它们的值

对每个参数,compact() 在当前的符号表中查找该变量名并将它添加到输出的数组中,变量名成为键名而变量的内容成为该键的值。

任何没有变量名与之对应的字符串都被略过。

$city  = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array("city", "state");
$result = compact("event", "nothing_here", $location_vars);
print_r($result);

Array
(
[event] => SIGGRAPH
[city] => San Francisco
[state] => CA
)

extract

从数组中将变量导入到当前的符号表

检查每个键名看是否可以作为一个合法的变量名,同时也检查和符号表中已有的变量名的冲突。

返回成功导入到符号表中的变量数目。

$var_array = [
"color" => "blue",
"size" => "medium",
"shape" => "sphere"
];
extract($var_array);
echo "$color, $size, $shape\n";

blue, medium, sphere

参考

  • PHP手册