Summary of Common Function Examples of PHP Array

  • 2021-10-27 06:43:45
  • OfStack

In this paper, the common functions of PHP array are described by examples. Share it for your reference, as follows:

Count the number and uniqueness of array elements

1. count() The array () function counts the number of elements in an array or the number of attributes in an object. For an array, the number of its elements is returned, and for other values, 1 is returned.


int count(mixed var[,int mode])

The first parameter is required, passing an array or object evaluated by individuals. The second parameter is optional. It specifies whether the mode of the function recursively calculates the number of elements in the multidimensional array. The possible values are 0 or 1. 0 is the default value. Multidimensional arrays are not detected. If 1 is detected, multidimensional arrays are detected

Example:


$a=array("a","b","c","d");
echo count($a);// Number of outputs 4
$b=array("a"=>array(1,2,3),"b"=>array(4,5,6));
echo count($b,1);// Output  8
echo count($b);// Output  2

2. array_count_values() The function is used to count the number of occurrences of all values in an array. The function has only 1 parameter


array array_count_values(array input)

Parameter specifies that 1 array is entered and 1 array is returned. The key name of its element is the value of the original array, and the key value is the number of times that the value appears in the original array


$array=array(1,"a",1,"b","a");
$newarray=array_count_values($array);
print_r($newarray);// Output array([1]=>2 [a]=>2 [b]=>1)

3. array_unique() The function deletes duplicate values from an array and returns a new array without duplicate values


array array_unique(array array)

Parameter needs to accept an array. When the values of several elements in the array are equal, only the first element is retained, other elements are deleted, and the key names in the returned new array remain unchanged. array_unique() Sort the values as strings, then keep only the first encountered key name for each value, and then ignore all subsequent key names

Example:


$a=array("a"=>1,"b"=>2,"c"=>1);
print_r(array_unique($a));// Output  array([a]=>1 [b]=>2)

4. array_filter() Function filters the elements in the array with callback function, and returns the array filtered by user-defined function


array array_filter(array input [,callback callback])

Parameter: Parameter 1 is required, Ask to input a filtered array, the second parameter is optional, the user-defined function name is passed in the form of string, if the user-defined filter function returns true, the current value of the operated array will be included in the returned result array, and the result will form a new array, if the original array is an associated array, the key name will remain unchanged.


function myFun($var){
 if($var % 2==0){
 return true;
 }
}
$array= array("a"=>1,"b"=>2,"c"=>3,"d"=>4);
print_r($array,"myFun");// Output  array([b]=>2 [d]=>4)

5. array_walk() Function applies callback processing to each element in the array, returning true if successful, or false if not


bool array_walk( array &array,callback funcname [,mixed userdata])

The first parameter is required, which requires input of an array handled by the specified callback function. The second parameter is passed to the user-defined callback function, which is used to operate the array passed to the first parameter

Example:


function myFunc1($value,$key){
 echo "key=$key value=$value"
}
$a=array("a"=>"lin1","b"=>"lin2","c"=>"lin3");
array_walk($a,"myFunc1");
function myFunc2($value,$key,$str){
 echo "$key $p $value";
}
array_walk($a,"myFunc2");
function myFunc3($value,$key){
 $value="lin.su";
}
array_walk($a,"myFunc3");
print_r($a);//$a  Yes 1 Array of references 

6. array_map() Function can handle multiple arrays, apply a callback function to the elements of a given array, and return the array after the user-defined function.


$a=array("a","b","c","d");
echo count($a);// Number of outputs 4
$b=array("a"=>array(1,2,3),"b"=>array(4,5,6));
echo count($b,1);// Output  8
echo count($b);// Output  2

0

Example:


$a=array("a","b","c","d");
echo count($a);// Number of outputs 4
$b=array("a"=>array(1,2,3),"b"=>array(4,5,6));
echo count($b,1);// Output  8
echo count($b);// Output  2

1

Output:

array(
[0]= > array([0]= > 1 [1]= > 2 [2]= > 3)
[1]= > array([0]= > 1 [1]= > 2 [2]= > 3)
)

For more readers interested in PHP related contents, please check the topics on this site: "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary", "php String (string) Usage Summary", "php Common Functions and Skills Summary", "PHP Error and Exception Handling Methods Summary", "PHP Basic Grammar Introduction Course", "php Object-Oriented Programming Introduction Course" and "PHP Mathematical Operation Skills Summary"

I hope this paper is helpful to everyone's PHP programming.


Related articles: