1 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
explode
使用一個(gè)字符串分割另一個(gè)字符串
$str = "11,22,33";$arr = explode(",",$str);
print_r($arr);Array(
[0] => 11
[1] => 22
[2] => 33)12345678910
implode
將一個(gè)一維數(shù)組的值轉(zhuǎn)化為字符串
$arr = [11,22,33];$str = implode("#",$arr);echo $str;11#22#3312345
split
用正則表達(dá)式將字符串分割到數(shù)組中
preg_split()函數(shù)使用了Perl兼容正則表達(dá)式語(yǔ)法,通常是比split()更快的替代方案。如果不需要正則表達(dá)式的威力,則使用explode()更快,這樣就不會(huì)招致正則表達(dá)式引擎的浪費(fèi)。
該函數(shù)在PHP7中已被廢棄
$str = "123dqaw76eqwv8902fwer12356";$arr = split("[a-z]+", $str);
print_r($arr);Array(
[0] => 123
[1] => 76
[2] => 8902
[3] => 12356)1234567891011
preg_split
通過(guò)一個(gè)正則表達(dá)式分隔字符串
如果你不需要正則表達(dá)式功能,可以有更快(并且更簡(jiǎn)單)的選擇比如 explode() 或 str_split()。
如果沒(méi)有成功匹配,將會(huì)返回一個(gè)數(shù)組,包含了單個(gè)元素,即輸入的字符串。
$str = "123dqaw76eqwv8902fwer12356";$arr = preg_split("/[a-z]+/i", $str);
print_r($arr);Array(
[0] => 123
[1] => 76
[2] => 8902
[3] => 12356)1234567891011
str_split
將字符串轉(zhuǎn)換為數(shù)組
$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
)12345678910111213141516171819202122232425
array_chunk
將一個(gè)數(shù)組分割成多個(gè)
$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
)
)1234567891011121314151617181920212223242526272829303132333435363738394041424344
array_combine
創(chuàng)建一個(gè)數(shù)組,用一個(gè)數(shù)組的值作為其鍵名,另一個(gè)數(shù)組的值作為其值
$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
)1234567891011
array_merge
合并一個(gè)或多個(gè)數(shù)組。一個(gè)數(shù)組中的值附加在前一個(gè)數(shù)組的后面,返回作為結(jié)果的數(shù)組。
別忘了數(shù)字鍵名將會(huì)被重新編號(hào)!
$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)1234567891011121314151617181920212223242526272829
如果你想完全保留原有數(shù)組并只想新的數(shù)組附加到后面,用 + 運(yùn)算符。如果兩個(gè)被合并的數(shù)組含有相同的key,則保留第一個(gè),忽略后邊的。
$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
)12345678910111213
array_merge_recursive
遞歸地合并一個(gè)或多個(gè)數(shù)組
如果輸入的數(shù)組中有相同的字符串鍵名,則這些值會(huì)被合并到一個(gè)數(shù)組中去,這將遞歸下去,因此如果一個(gè)值本身是一個(gè)數(shù)組,本函數(shù)將按照相應(yīng)的條目把它合并為另一個(gè)數(shù)組。然而,如果數(shù)組具有相同的數(shù)組鍵名,后一個(gè)值將不會(huì)覆蓋原來(lái)的值,而是附加到后面。
$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)123456789101112131415161718192021222324252627282930313233343536373839404142
compact
建立一個(gè)數(shù)組,包括變量名和它們的值
對(duì)每個(gè)參數(shù),compact() 在當(dāng)前的符號(hào)表中查找該變量名并將它添加到輸出的數(shù)組中,變量名成為鍵名而變量的內(nèi)容成為該鍵的值。
任何沒(méi)有變量名與之對(duì)應(yīng)的字符串都被略過(guò)。
$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
)12345678910111213
extract
從數(shù)組中將變量導(dǎo)入到當(dāng)前的符號(hào)表
檢查每個(gè)鍵名看是否可以作為一個(gè)合法的變量名,同時(shí)也檢查和符號(hào)表中已有的變量名的沖突。
返回成功導(dǎo)入到符號(hào)表中的變量數(shù)目。
$var_array = [ "color" => "blue", "size" => "medium", "shape" => "sphere"];
extract($var_array);echo "$color, $size, $shape\n";
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)