Examples of array processing functions in PHP Summary, PHP array function Instance _php tutorial

Source: Internet
Author: User

Example summary of array processing function in PHP, example of PHP array function

This article summarizes the array processing functions in PHP. Share to everyone for your reference, as follows:

<?php//Change the case of the array key $arr 1=array ("a" = "Lamp", "db" = "database", "LANGUAGE" and "PHP"); Print_r (Array_change_key_case ($arr 1,case_upper)); echo "
"; Print_r (Array_change_key_case ($arr 1,case_lower)); echo "
"; Splits an array into a number of third parameters to set whether to retain the key $arr 2=array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '); Print_r (Array_chunk ($arr 2,2,true)); echo "
"; Print_r (Array_chunk ($arr 2,2,false)); echo "
"; Array Array_diff_assoc (array $array 1, array $array 2 [, array $ ...]) returns an array that contains all of the values in array1 but not in any other parameter array// The key difference is also $arr 3=array (' a ' = ' = ' green ', ' b ' = ' brown ', ' c ' = ' red '); $arr 4=array (' a ' = ' green ', ' yellow ', ' red '); Print_r (Array_diff_assoc ($arr 3, $arr 4)); echo "
"; Array_diff (array $array 1, array $array 2 [, array $ ...])//Returns an array that contains all the values in array1 but not in any//other parameter array, the keys are not the same print_ R (Array_diff ($arr 3, $arr 4)); echo "
"; Array_fill (int $start _index, int $num, mixed $value)//Use the value parameter to populate an array with num entries,//key names starting with the Start_index parameter specified. Print_r (Array_fill ( -5,8, "banana")); echo "
"; Array_flip (array $trans)//Returns a reversed array, such as the key name in trans becomes a value,//and the value in trans is a key name. $arr 5=array (' a ' = ' 1 ', "b" = "2", "C", "D", "E"); Print_r (Array_flip ($arr 5)); echo "
"; Array_map (callback $callback, array $arr 1 [, Array $ ...])//Returns an array that contains all the cells in the arr1 that have been callback after//. The number of arguments callback accepts should be the same as the number of arrays passed to the ARRAY_MAP ()//function. function cube ($n) {return $n * $n;} $arr 6=array (1,2,3,4,5); Print_r (Array_map ("Cube", $arr 6)); echo "
"; Array_merge_recursive (array $array 1 [, Array $ ...])//merge the cells of one or more arrays, and the values in an array are appended to the previous array//. Returns an array as the result. If the input array has the same string key name,//Then the values are merged into an array, which will recursively go down, so if a value itself//is an array, this function will merge it into another array according to the corresponding entry. However, if the//fruit array has the same array key name, the latter value will not overwrite the original value, but append to//behind. $arr 7=array ("Color" =>array ("favorite" = "Red"), 5); $arr 8=array (10,array ("favorite" = "yellow", "blue")); Print_r (Array_merge_recursive ($arr 7, $arr 8)); echo "
"; Array_reduce (array $input, callback $function [, int $initial]//Iterates the callback function functions into each cell in the input array, simplifying the array//To A single value. If an optional parameter initial is specified, the parameter will be treated as the first//value in the array or, if the array is empty, as the final return value. If the array is empty and no//has passed the initial parameter, Array_reduce () returns NULL. function Rsum ($v, $w) {$v + = $w; return $v;} function Rmul ($v, $w) {$v *= $w; return $v;} $a =array (1,2,3,4,5); $x =array (); $b =array_reduce ($a, "rsum"); $c =array_reduce ($a, "Rmul", 10); $d =array_reduce ($x, "Rsum", 1); echo $b. " \t\t ". $c." \t\t ". $d." \ n "; echo "
"; Array_replace (array & $array, array & $array 1 [, Array & $array 2 [, Array &$ ...])//function replace the first with the value of the following array element The value of array arrays. If a key exists in the first//array and also exists in the second, its value is replaced by the value in the second array. If a//key exists in the second array, but does not exist in the first array, the//element is created in the first array. If a key exists only in the first array, it will remain unchanged. If multiple replacements//groups are passed, they are processed sequentially, followed by an array that overrides the previous value. $base =array ("Orange", "banana", "apple", "raspberry"); $replacements =array (0=> "Pineapple",4=> "cherry"); $replacements 2=array (0=> "Grape"); Print_r (Array_replace ($base, $replacements, $replacements 2)); echo "
"; Array_splice (array & $input, int $offset [, int $length [, array $ replacement]])//Put the input array by offset and length The specified cell is removed, and if the replacement//parameter is supplied, the cells in the replacement array are replaced. Returns an array containing the cells that have been removed//. Note the numeric key names in input are not retained. If length is omitted, all parts from/offset to the end of the divisor group are moved. If length is specified and positive, then so many units//are removed. If length is specified and is negative, all cells in the middle of the countdown from offset to the end of the array are removed. Tip: Use COUNT ($input) as length when you want to remove all cells from offset to//array at the end of the replacement. $input =array ("Red", "green", "blue", "yellow"); Array_splice ($input, 1,-1); Print_r ($input); echo "
"; Key (array & $array)//Returns the key name of the current cell in the array. $fruit =array ("Fruit1" and "Apple", "fruit2" and "Orange", "fruit3" and "Grape", "FRUIT4" and "Apple", "fruit5" = > "Apple"); while ($fruit _name=current ($fruit)) {if ($fruit _name== ' Apple ') {echo key ($fruit). "
"; } Next ($fruit); } echo "
";? >

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP Array function array_key_exists () Summary
    • A summary of some common functions of increment, delete and interpolation of logarithm groups in PHP
    • Example Summary of PHP arrays and explode functions
    • PHP recursively invokes an array value and uses it to execute the specified function's method
    • PHP Array Correlation Function Summary
    • Php method for merging two arrays with the Array_merge () function
    • PHP adds multiple variables to the end of the array by using the Array_push () function
    • Php method for adding multiple variables to the front of the array via the Array_unshift function
    • Php method for removing the first element of an array by using the Array_shift () function

http://www.bkjia.com/PHPjc/1089939.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089939.html techarticle An example of an array processing function in PHP summarizes the example of a PHP array function, which summarizes the array processing functions in PHP. Share to everyone for your reference, as follows: PHP//change array ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.