SoFunction
Updated on 2025-03-02

Summary of the commonly used array array function examples of php [assignment, splitting, merge, calculation, addition, deletion, query, judgment, sorting]

This example summarizes the commonly used array array function of PHP. Share it for your reference, as follows:

array_combine

Function: Use the value of one array as the key name of the new array, and the value of another array as the value of the new array

Case:

<?php
$a = array("one","two","three");
$b = array("one","two","three");
$c = array_combine($a,$b);
print_r($c);
/**result
 *Array ( [one] => one [two] => two [three] => three )
 */

array_chunk

Function: Split number to form multiple arrays

<?php
$input_array = array("a"=>"apple","b"=>"blue","c","d","e");
echo "<pre>";
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2,True));
echo "</pre>";
/**result
 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

Function: count the number of times the value in the array appears

<?php
$input_array = array("a"=>"apple","b"=>"blue","c","d","e");
echo "<pre>";
print_r(array_count_values($input_array));
echo "</pre>";
/**result
 Array
 (
  [apple] => 1
  [blue] => 1
  [c] => 1
  [d] => 1
  [e] => 1
 )
 */

array_diff

Function: Remove the data from the second array from the first array and return the remaining content as the result

<?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>";
print_r($result);// Remove the remaining ones from array 1 from array 2print_r($result2);// Remove the remaining ones from array 1 from array 2echo "</pre>";
/**result
 Array
 (
  [b] => blue
  [2] => e
 )
 Array
 (
  [3] => f
 )
 */

array_map

Function: Execute the callback function into the array

<?php
//Define callback functionfunction cube($n){
 return ($n*$n*$n);
}
$a = array(1,2,3,4,5);
$b = array_map("cube",$a);
echo "<pre>";
print_r($b);
echo "</pre>";
/**result
 Array
 (
  [0] => 1
  [1] => 8
  [2] => 27
  [3] => 64
  [4] => 125
 )
 */

array_merge

Function: Merge one or more arrays

Note: If there is a key name followed by the same key name, the previous content will be overwritten, and the key name is a number will be added to the back

<?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>";
print_r($result1);
print_r($result2);
echo "</pre>";
/**result
 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

Function: Exclude the last element of the array and return the content of the removed element

<?php
$stack = array("orange","banana","apple","1");
$last = array_pop($stack);
echo "<pre>";
print_r($stack);
print_r($last);
echo "</pre>";
/**result
 Array
 (
  [0] => orange
  [1] => banana
  [2] => apple
 )
 1
 */

array_push

Function: Press multiple units into the end of the array, and return the number of arrays afterwards

<?php
$stack = array("orange","banana");
$count = array_push($stack,"apple","red","blue");
echo "<pre>";
print_r($stack);
print_r($count);
echo "</pre>";
/**result
 Array
 (
  [0] => orange
  [1] => banana
  [2] => apple
  [3] => red
  [4] => blue
 )
 5
 */

array_rand

Function: Get random key names

<?php
$input = array("orange","banana","apple","red","blue");
$rand = array_rand($input,2);;
print_r($rand);
$rand = array_rand($input,3);
print_r($rand);
/**result
 Array
 (
  [0] => 1
  [1] => 4
 )
 Array
 (
  [0] => 0
  [1] => 1
  [2] => 3
 )
 */

array_search

Function: Query the content in the array, return the key value, if there are multiple matches, return the first match

<?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>";
/**result
 blue
 red
 */

array_shift

Function: Remove the beginning element, contrary to array_pop

<?php
$fruit = array("milk","orange","banana","apple");
$top = array_shift($fruit);
print_r($top);
echo "<br>";
print_r($fruit);
/**result
 milk
 Array ( [0] => orange [1] => banana [2] => apple )
 */

array_unique

Function: Remove duplicate elements of the array, retain the first occurrence, including the key name and value

<?php
$input = array("a"=>"green","red","b"=>"green","blue","c"=>"red");
$result = array_unique($input);
print_r($result);
echo "<br>";
print_r($input);
/**result
 Array ( [a] => green [0] => red [1] => blue )
 Array ( [a] => green [0] => red [b] => green [1] => blue [c] => red )
 */

array_slice

Function: Extract some elements from the array

<?php
$input = array("a","b","c","d","e");
$output = array_slice($input,2);//When the second parameter does not exist, it means that the last element is takenprint_r($output);
echo "<br>";
$output = array_slice($input,-2,1);//When the second parameter is a positive number, it represents a number; the penultimate is -1, and the penultimate is -2print_r($output);
echo "<br>";
$output = array_slice($input,0,3);
print_r($output);
echo "<br>";
$output = array_slice($input,2,-1);//When the second parameter is a negative number, it indicates the position, which bit is taken, does not include itselfprint_r($output);
echo "<br>";
$output = array_slice($input,2,-1,true);//When the third parameter is true, the original key value is retainedprint_r($output);
echo "<br>";
/**result
 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 )
 */

count

Function: Return the number of elements in the array, and the element is calculated as an array

<?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;
/**result
 4
 5
 */

current

Function: Get the current pointer to the element

<?php
$array = array("foot","bike","car","plane");
$result = current($array);
echo $result."<br>";
next($array);// Make the pointer point to the next element$result = current($array);
echo $result."<br>";
prev($array);// Make the pointer point to the previous element$result = current($array);
echo $result."<br>";
end($array);// Make the pointer point to the last element$result = current($array);
echo $result."<br>";
/**result
 foot
 bike
 foot
 plane
 */

in_array

Function: Check whether a value exists in the array, returns True, but not False

<?php
$os_list = array("Mac","NT","Irix","Linux");
if(in_array("Irix",$os_list)){
 echo "Irix exists in the current operating system list";
}else{
 echo "Irix does not exist in the current operating system list";
}
echo "<br>";
if(in_array("mac",$os_list)){
 echo "There is mac in the current operating system list";
}else{
 echo "Mac does not exist in the current operating system list";
}
echo "<br>";
/**result
 Irix exists in the current operating system list
 Mac does not exist in the current operating system list
 */

list

Function: Assign information in the array to multiple variables

<?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>";
/**result
 red,blue,green
 red,green
 green
 */

shuffle

Function: disrupt the array

<?php
$numbers = range(1,5);//Generate a random arrayprint_r($numbers);
echo "<br/>";
shuffle($numbers);//Suppress the arrayprint_r($numbers);
/**result
 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
 Array ( [0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3 )
 */

array_keys

Function: Get the key name of the array, the second parameter can specify to obtain a certain element

<?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>";
/**result
 Array ( [0] => 0 [1] => color )
 Array ( [0] => 0 [1] => 3 [2] => 4 )
 Array ( [0] => color [1] => size )
 */

array_reverse

Function: Get the reverse of the array

<?php
$input = array("php",3.0,array("green","red"));
$result = array_reverse($input); //Stop the key name$result_keyed = array_reverse($input,TRUE);//Reserve key nameprint_r($result);
print_r($result_keyed);
/**result
 Array
 (
  [0] => Array
   (
    [0] => green
    [1] => red
   )
  [1] => 3
  [2] => php
 )
 Array
 (
  [2] => Array
   (
    [0] => green
    [1] => red
   )
  [1] => 3
  [0] => php
 )
 */

arsort

Function: reverse sorting, index unchanged

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
arsort($fruits);//Sort in reverse by characters or numbersforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**result
 b = orange
 a = lemon
 c = banana
 d = apple
 */

asort

Function: Forward sorting

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
arsort($fruits);//Sort in reverse by characters or numbersforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
echo "<p>";
asort($fruits);//Sort by character or numberforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**result
 b = orange
 a = lemon
 c = banana
 d = apple
 d = apple
 c = banana
 a = lemon
 b = orange
 */

krsort

Function: Reverse sorting by key name

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
krsort($fruits);//Sort in reverse by key name or numberforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**result
 d = apple
 c = banana
 b = orange
 a = lemon
 */

ksort

Function: Forward sorting by key name

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
ksort($fruits);//Forward sorting or number by key nameforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**result
 a = lemon
 b = orange
 c = banana
 d = apple
 */

rsort

Function: Sorting in reverse by value, changing key name

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
rsort($fruits);//Sort in reverse or number according to the value, change the key nameforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**result
 0 = orange
 1 = lemon
 2 = banana
 3 = apple
 */

sort

Function: Forward sorting by value, change the key name

<?php
$fruits = array(
 "a"=>"lemon",
 "b"=>"orange",
 "c"=>"banana",
 "d"=>"apple",
 );
sort($fruits);//Sort in reverse or number according to the value, change the key nameforeach($fruits as $key=>$val){
 echo "$key = $val<br>";
}
/**result
 0 = apple
 1 = banana
 2 = lemon
 3 = orange
 */

For more information about PHP related content, please check out the topic of this site:Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《Summary of common functions and techniques for php》、《Summary of PHP error and exception handling methods》、《Introduction to PHP basic syntax》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.