Die Beispiele in diesem Artikel fassen die häufig verwendeten Array-Array-Funktionen in PHP zusammen. Teilen Sie es als Referenz mit allen. Die Details lauten wie folgt:
array_combine
Funktion: Verwenden Sie den Wert eines Arrays als Schlüsselnamen des neuen Arrays und den Wert des anderen Array als Wert des neuen Arrays
Fall:
<?php $a = array("one","two","three"); $b = array("一","二","三"); $c = array_combine($a,$b); print_r($c); /**结果 *Array ( [one] => 一 [two] => 二 [three] => 三 ) */
array_chunk
Funktion: Array in mehrere aufteilen arrays
<?php $input_array = array("a"=>"apple","b"=>"blue","c","d","e"); echo "<pre class="brush:php;toolbar:false">"; print_r(array_chunk($input_array, 2)); print_r(array_chunk($input_array, 2,True)); echo ""; /**结果 Array ( [0] => Array ( [0] => apple [1] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [a] => apple [b] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [2] => e ) ) */
array_count_values
Funktion: Zählt die Anzahl der Vorkommen von Werten in einem Array
<?php $input_array = array("a"=>"apple","b"=>"blue","c","d","e"); echo "<pre class="brush:php;toolbar:false">"; print_r(array_count_values($input_array)); echo ""; /**结果 Array ( [apple] => 1 [blue] => 1 [c] => 1 [d] => 1 [e] => 1 ) */
array_diff
Funktion: Entfernen Sie die Daten im zweiten Array aus dem ersten Array und geben Sie den verbleibenden Inhalt als Ergebnis zurück
<?php $array1 = array("a"=>"apple","b"=>"blue","c","d","e"); $array2 = array("apple","c","d","f"); $result = array_diff($array1, $array2); $result2 = array_diff($array2, $array1); echo "<pre class="brush:php;toolbar:false">"; print_r($result);//数组1中去掉数组2中剩下的 print_r($result2);//数组2中去掉数组1中剩下的 echo ""; /**结果 Array ( [b] => blue [2] => e ) Array ( [3] => f ) */
array_map
Funktion: Callback-Funktion im Array ausführen
<?php //定义回调函数 function cube($n){ return ($n*$n*$n); } $a = array(1,2,3,4,5); $b = array_map("cube",$a); echo "<pre class="brush:php;toolbar:false">"; print_r($b); echo ""; /**结果 Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) */
array_merge
Funktion: Ein oder mehrere Arrays zusammenführen
Hinweis: Wenn sich auf der Rückseite Schlüssel mit denselben Schlüsselnamen befinden, wird der vorherige Inhalt überschrieben und die Schlüsselnamen mit Nummern werden überschrieben hinten hinzugefügt
<?php $array1 = array("color"=>"red",2,4); $array2 = array("a","b","color"=>"green","shape"=>"trapezoid",4); $result1 = array_merge($array1,$array2); $result2 = array_merge_recursive($array1,$array2); echo "<pre class="brush:php;toolbar:false">"; print_r($result1); print_r($result2); echo ""; /**结果 Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Array ( [color] => Array ( [0] => red [1] => green ) [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) */
array_pop
Funktion: Das letzte Element des Arrays entfernen und den Inhalt des entfernten Elements zurückgeben
<?php $stack = array("orange","banana","apple","1"); $last = array_pop($stack); echo "<pre class="brush:php;toolbar:false">"; print_r($stack); print_r($last); echo ""; /**结果 Array ( [0] => orange [1] => banana [2] => apple ) 1 */
array_push
Funktion: Mehrere Einheiten an das Ende des Arrays schieben und die Anzahl der Arrays nach
<🎜 zurückgeben ><?php $stack = array("orange","banana"); $count = array_push($stack,"apple","red","blue"); echo "<pre class="brush:php;toolbar:false">"; print_r($stack); print_r($count); echo ""; /**结果 Array ( [0] => orange [1] => banana [2] => apple [3] => red [4] => blue ) 5 */
<?php $input = array("orange","banana","apple","red","blue"); $rand = array_rand($input,2);; print_r($rand); $rand = array_rand($input,3); print_r($rand); /**结果 Array ( [0] => 1 [1] => 4 ) Array ( [0] => 0 [1] => 1 [2] => 3 ) */
<?php $array = array("blue"=>"b","red"=>"r","green","r"); $key = array_search('b', $array); echo $key; echo "<br>"; $key = array_search('r', $array); echo $key; echo "<br>"; /**结果 blue red */
<?php $fruit = array("milk","orange","banana","apple"); $top = array_shift($fruit); print_r($top); echo "<br>"; print_r($fruit); /**结果 milk Array ( [0] => orange [1] => banana [2] => apple ) */
<?php $input = array("a"=>"green","red","b"=>"green","blue","c"=>"red"); $result = array_unique($input); print_r($result); echo "<br>"; print_r($input); /**结果 Array ( [a] => green [0] => red [1] => blue ) Array ( [a] => green [0] => red [b] => green [1] => blue [c] => red ) */
<?php $input = array("a","b","c","d","e"); $output = array_slice($input,2);//第二个参数没有时,表示取到最后一个元素 print_r($output); echo "<br>"; $output = array_slice($input,-2,1);//第二个参数是正数时,表示个数;倒数第一个是-1,倒数第二个是-2 print_r($output); echo "<br>"; $output = array_slice($input,0,3); print_r($output); echo "<br>"; $output = array_slice($input,2,-1);//第二个参数是负数时,表示位置,取到哪一位,不包括本身 print_r($output); echo "<br>"; $output = array_slice($input,2,-1,true);//第三个参数为true时,保留原有的键值 print_r($output); echo "<br>"; /**结果 Array ( [0] => c [1] => d [2] => e ) Array ( [0] => d ) Array ( [0] => a [1] => b [2] => c ) Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d ) */
<?php $input = array("a","b","c",array("d","e")); $count = count($input); echo $count; echo "<br>"; $input = array("a","b","c","d","e"); $count = count($input); echo $count; /**结果 4 5 */
<?php $array = array("foot","bike","car","plane"); $result = current($array); echo $result."<br>"; next($array);//使指针指向下一个元素 $result = current($array); echo $result."<br>"; prev($array);//使指针指向前一个元素 $result = current($array); echo $result."<br>"; end($array);//使指针指向最后一个元素 $result = current($array); echo $result."<br>"; /**结果 foot bike foot plane */
<?php $os_list = array("Mac","NT","Irix","Linux"); if(in_array("Irix",$os_list)){ echo "当前操作系统列表中存在Irix"; }else{ echo "当前操作系统列表中不存在Irix"; } echo "<br>"; if(in_array("mac",$os_list)){ echo "当前操作系统列表中存在mac"; }else{ echo "当前操作系统列表中不存在mac"; } echo "<br>"; /**结果 当前操作系统列表中存在Irix 当前操作系统列表中不存在mac */
<?php $info = array("red","blue","green"); list($flag,$sky,$grassland) = $info; echo "$flag,$sky,$grassland"; echo "<br>"; list($flag,,$grassland) = $info; echo "$flag,$grassland"; echo "<br>"; list(,,$grassland) = $info; echo "$grassland"; echo "<br>"; /**结果 red,blue,green red,green green */
<?php $numbers = range(1,5);//生成一个随机数组 print_r($numbers); echo "<br/>"; shuffle($numbers);//打乱数组 print_r($numbers); /**结果 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( [0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3 ) */
< zu erhalten 🎜>
<?php $array = array(0=>100,"color"=>"red"); print_r(array_keys($array)); echo "<br>"; $array = array("blue","red","green","blue","blue"); print_r(array_keys($array,"blue")); echo "<br>"; $array = array("color"=>array("blue","red","green"),"size"=>array("small","medium","large")); print_r(array_keys($array)); echo "<br>"; /**结果 Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size ) */
array_reverse
Funktion: Die Umkehrung des Arrays ermitteln
<?php $input = array("php",3.0,array("green","red")); $result = array_reverse($input); //打乱键名 $result_keyed = array_reverse($input,TRUE);//保留键名 print_r($result); print_r($result_keyed); /**结果 Array ( [0] => Array ( [0] => green [1] => red ) [1] => 3 [2] => php ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 3 [0] => php ) */
arsort
Funktion: Umgekehrte Sortierung, der Index bleibt unverändert
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); arsort($fruits);//按照字符逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 b = orange a = lemon c = banana d = apple */
asort
Funktion: durchführen Vorwärtssortierung
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); arsort($fruits);//按照字符逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } echo "<p>"; asort($fruits);//按照字符正向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 b = orange a = lemon c = banana d = apple d = apple c = banana a = lemon b = orange */
krsort
Funktion: Taste drücken Rückwärtssortierung nach Tastenname
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); krsort($fruits);//按照键名逆向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 d = apple c = banana b = orange a = lemon */
rsort
Funktion: Umgekehrte Sortierung nach Wert, Änderung des Schlüsselnamens
<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); ksort($fruits);//按照键名正向排序或数字 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 a = lemon b = orange c = banana d = apple */
sortieren
Funktion: Vorwärtssortierung nach Wert, Änderung des Schlüsselnamens
Hilft.<?php $fruits = array( "a"=>"lemon", "b"=>"orange", "c"=>"banana", "d"=>"apple", ); rsort($fruits);//按照值进行逆向排序或数字,键名改变 foreach($fruits as $key=>$val){ echo "$key = $val<br>"; } /**结果 0 = orange 1 = lemon 2 = banana 3 = apple */
Weitere Beispiele für häufig verwendete Array-Array-Funktionen in PHP [Zuweisen, Teilen, Zusammenführen, Berechnen, Hinzufügen, Löschen, Abfragen, Beurteilen, Sortieren] finden Sie auf der chinesischen PHP-Website für verwandte Artikel!