Detailed explanation of the usage of Array array function in php (1/2)_PHP tutorial

WBOY
Release: 2016-07-13 10:50:18
Original
935 people have browsed it

Array is a special data type in php. At the same time, php also provides us with a large number of array operation functions, so that we can feel stress-free when operating arrays. Below I summarize the introductory notes of php array array. Share it with you.

Introduction: This article introduces the system functions used to perform various operations on arrays in the PHP manual. It can be said that arrays play an important role in PHP, so there are many functions. Below, Tianya lists the most commonly used ones. Detailed description.

array_change_key_case — Returns an array whose string keys are all lowercase or uppercase

array array_change_key_case ( array $input [, int $case ] )

$case can be CASE_UPPER or CASE_LOWER (default)

 代码如下 复制代码
$phpha = array('Home'=>'http://www.bKjia.c0m', 'Blog'=>'http://www.bKjia.c0m');
    $phpha_upper = array_change_key_case($phpha, CASE_UPPER);
    $phpha_lower = array_change_key_case($phpha, CASE_LOWER); //默认情况
    print_r($phpha_upper);
    print_r($phpha_lower);
    ?>
    //结果:
    Array
    (
    [HOME] => http://www.bKjia.c0m
    [BLOG] => http://www.bKjia.c0m
    )
    Array
    (
    [home] => http://www.bKjia.c0m
    [blog] => http://www.bKjia.c0m
    )

array_chunk — Split an array into multiple

array array_chunk ( array $input , int $size [, bool $preserve_keys ] )

array_chunk() splits an array into multiple arrays, where the number of cells in each array is determined by size. The last array may have a few fewer elements. The resulting array is a cell in a multidimensional array, with indexes starting from zero.
Setting the optional preserve_keys parameter to TRUE causes PHP to preserve the original key names in the input array. If you specify FALSE, each result array will be indexed with a new number starting from zero. The default value is FALSE.

The code is as follows Copy code
代码如下 复制代码
$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
    )
    
    )
$input_array = array('a', 'b', 'c', 'd', 'e'); Print_r(array_chunk($input_array, 2)); Print_r(array_chunk($input_array, 2, TRUE)); ?> //Result: 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 — Create an array using the values ​​of one array as its keys and the values ​​of another array as its values ​​

array array_combine ( array $keys , array $values ​​)

Returns an array with values ​​from the keys array as key names and values ​​from the values ​​array as corresponding values.
Returns FALSE if the two arrays have different numbers of elements or if the arrays are empty.

The code is as follows Copy code
代码如下 复制代码
$key = array('Home', 'Blog');
$key2 = array('Home', 'Blog', 'BBS');
$phpha = array('http://www.bKjia.c0m', 'http://www.bKjia.c0m');
$phpha_combine = array_combine($key, $phpha);
$phpha_combine_wrong = array_combine($key2, $phpha);
print_r($phpha_combine);
print_r($phpha_combine_wrong);
?>
    //结果:
    Array
    (
    [Home] => http://www.bKjia.c0m
    [Blog] => http://www.bKjia.c0m
    )
   
$key = array('Home', 'Blog');

$key2 = array('Home', 'Blog', 'BBS');
$phpha = array('http://www.bKjia.c0m', 'http://www.bKjia.c0m');

$phpha_combine = array_combine($key, $phpha);

$phpha_combine_wrong = array_combine($key2, $phpha);

Print_r($phpha_combine);

Print_r($phpha_combine_wrong);

?>

//Result:

Array

(

[Home] => http://www.bKjia.c0m
 代码如下 复制代码
  $phpha = array('hello', 'world', 'tianya', 'hello', 'world');
$phpha_result = array_count_values($phpha);
print_r($phpha_result);
?>
    //结果:
    Array
    (
    [hello] => 2
    [world] => 2
    [tianya] => 1
    )
[Blog] => http://www.bKjia.c0m ) // You can see that array_combine() reported an error for the second time, pointing out that the number of members of the two arrays is not equal Warning: Both parameters should have an equal number of elements in F:Program FilesPHPNOWhtdocsindex.php on line 31 array_count_values ​​— Count the number of occurrences of all values ​​in an array array array_count_values ​​(array $input) array_count_values() returns an array that uses the value in the input array as the key name and the number of times the value appears in the input array as the value.
The code is as follows Copy code
$phpha = array('hello', 'world', 'tianya', 'hello', 'world');<🎜> $phpha_result = array_count_values($phpha);<🎜> Print_r($phpha_result);<🎜> ?> //Result: Array ( [hello] => 2 [world] => 2 [tianya] => 1 )

array_diff — Calculate the difference of arrays
array_diff_key — Compute the difference of an array using key name comparison
array_diff_ukey — Use callback function to compare key names to calculate the difference of array
array_diff_assoc — Compute the difference of an array with index checking
array_diff_uassoc — Compute the difference of an array using a user-supplied callback function with index checking

The code is as follows Copy code
//array_diff() returns an array, the The array contains everything in array1
 代码如下 复制代码
  //array_diff() 返回一个数组,该数组包括了所有在array1中
    //但是不在任何其它参数数组中的值。注意键名保留不变。
    $array1 = array("a" => "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array1, $array2);
    print_r($result);
    ?>
    //结果:
    Array
    (
    [1] => blue
    )
    //本函数和array_diff()相同只除了比较是根据键名而不是值来进行的。
    $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
    print_r(array_diff_key($array1, $array2));
    ?>
    //结果:
    Array
    (
    [red] => 2
    [purple] => 4
    )
    //注意和 array_diff() 不同的是键名也用于比较。
    $array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
    $array2 = array ("a" => "green", "yellow", "red");
    print_r(array_diff_assoc($array1, $array2));
    ?>
    //结果:
    Array
    (
    [b] => brown
    [c] => blue
    [0] => red
    )
//But a value that is not in any other parameter array. Note that the key names remain unchanged. $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); Print_r($result); ?> //Result: Array ( [1] => blue ) //This function is the same as array_diff() except that the comparison is based on key names instead of values. $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); Print_r(array_diff_key($array1, $array2)); ?> //Result: Array ( [red] => 2 [purple] => 4 ) //Note that unlike array_diff(), key names are also used for comparison. $array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array ("a" => "green", "yellow", "red"); Print_r(array_diff_assoc($array1, $array2)); ?> //Result: Array ( [b] => brown [c] => blue [0] => red )

array_fill — fills an array with the given values
array_fill_keys — Fill an array with values, specifying keys

array_filter — Use a callback function to filter elements in an array

The code is as follows Copy code
 代码如下 复制代码

    function func_check($i){
return $i > 3 ? TRUE : FALSE;
    }
    $array1 = array(2, 3, 5, 6);
    $array2 = array(NULL, '', 'hello');
    $array3 = array_filter($array1, 'func_check');
    $array4 = array_filter($array2);
    //函数func_check()用来判断给定的值,返回TRUE或FALSE
    //返回TRUE,则$array1中的值则会返回且键名不变,否则被过滤掉
    print_r($array3);
    //如果没有指定回调函数,则默认过滤掉array2中为等值为FALSE的成员
    //进行类型转换。因此本函数常用量顾虑掉数组中的空成员。
    print_r($array4);
    ?>
    //结果:
    Array
    (
    [2] => 5
    [3] => 6
    )
    Array
    (
    [2] => hello
    )

Function func_check($i){

Return $i > 3 ? TRUE : FALSE;

}

$array1 = array(2, 3, 5, 6);
 代码如下 复制代码
   //如果同一个值出现了多次,则最后一个键名将作为它的值,所有其它的都丢失了。
    $trans = array("a" => 1, "b" => 1, "c" => 2);
    $trans = array_flip($trans);
    print_r($trans);
    ?>
    //结果:
    Array
    (
    [1] => b
    [2] => c
    )
$array2 = array(NULL, '', 'hello'); $array3 = array_filter($array1, 'func_check'); $array4 = array_filter($array2); //The function func_check() is used to judge the given value and returns TRUE or FALSE //Return TRUE, the value in $array1 will be returned and the key name remains unchanged, otherwise it will be filtered out Print_r($array3); //If no callback function is specified, members in array2 with the equivalent value of FALSE will be filtered out by default //Perform type conversion. Therefore, this function often considers empty members in the array. Print_r($array4); ?> //Result: Array ( [2] => 5 [3] => 6 ) Array ( [2] => hello )
array_flip — swap keys and values ​​in an array
The code is as follows Copy code
//If the same value appears multiple times , the last key name will be used as its value, and all others are lost. $trans = array("a" => 1, "b" => 1, "c" => 2); $trans = array_flip($trans); Print_r($trans); ?> //Result: Array ( [1] => b [2] => c )

array_intersect — Calculate the intersection of arrays
array_intersect_assoc — Compute the intersection of arrays with index checking
array_intersect_uassoc — Compute the intersection of arrays with index checking, using a callback function to compare the indices
array_intersect_key — Compute the intersection of arrays using key name comparison
array_intersect_ukey — Compute the intersection of arrays using callback functions to compare keys

The code is as follows Copy code
 代码如下 复制代码
  $array1 = array("a" => "green", "red", "blue");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_intersect($array1, $array2);
    print_r($result);
    ?>
    //结果:
    Array
    (
    [a] => green
    [0] => red
    )
    //注意array_intersect_assoc()和array_intersect()不同的是键名也用于比较。
    $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
    $array2 = array("a" => "green", "yellow", "red");
    $result = array_intersect_assoc($array1, $array2);
    print_r($result);
    ?>
    //结果:
    Array
    (
    [a] => green
    )
$array1 = array("a" => "green", "red", "blue");

$array2 = array("b" => "green", "yellow", "red");

$result = array_intersect($array1, $array2);

Print_r($result);

?> Array ( [a] => green [0] => red ) //Note that the difference between array_intersect_assoc() and array_intersect() is that key names are also used for comparison. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_intersect_assoc($array1, $array2); Print_r($result);
?> //Result: Array
(
[a] => green ) 1 2 http://www.bkjia.com/PHPjc/632651.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632651.htmlTechArticleArray is a special data type in php, and php also provides us with a large number of array operation functions , so that we feel stress-free when operating on arrays. I summarized it below...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!