Learning notes for PHP array

Source: Internet
Author: User
Tags php error
. The code is as follows:


Header (' content-type:text/html; Charset=utf-8 ');
Echo ' <pre> ';
function Var_array ($array)
{
Echo ' <pre> ';
Var_dump ($array);
Echo ' </pre> ';
}
function Printr ($array)
{
Echo ' <pre> ';
Print_r ($array);
Echo ' </pre> ';
}
function Getarr ($sNum, $eNum =1, $step =1)
{
$arr = Range ($sNum, $eNum, $step);
$REARR = Array ();
foreach ($arr as $v)
{
$REARR [$v] = rand (0,10);
}
Unset ($arr);
return $REARR;
}
/**
* Array Exercises
*/
//---------------------------------------------
Array_change_key_case () changes the uppercase and lowercase letters of an array index, determined by the last parameter: Case_upper (converted to uppercase), Case_lower (default conversion to lowercase)
$EXPARR = Array (
' First ' = ' 1 ',
' SEcoNd ' = ' 2 ',
' Third ' = Array (
' Huima ' = ' 3 ',
' Nengzhuanma ' = ' 5 ',
)
);
Printr (Array_change_key_case ($EXPARR));//convert All to lowercase
Printr (Array_change_key_case ($expArr [' third '], case_upper));//convert all uppercase to only one index key in the $exparr array
Summary: This function affects only one layer of the array. And will not have an effect on the original array
Echo ' <br/>//---------------------------------------------
Array_chunk ($array, $size, False)
Splits an array into a multidimensional array, and size determines that the array becomes a multidimensional array every $size, True/false determines whether the new array's key values follow the original array's keys
$EXPARR = Array (' 4 ', ' 2 ', ' 6 ', ' d ', ' 2 ');
Printr (Array_chunk ($EXPARR, 3));
Summary: This function affects only one layer of the array. And will not have an effect on the original array
Echo ' <br/>//---------------------------------------------
Array_combine ($KEYARR, $VALARR)
Synthesize two array groups into an array, $keyArr as a key, $valArr as a value
$expKey = Array (' g ', ' d ', ' t ');
$expVal = Array (' 5 ', ' 8 ', ' 7 ');
Printr (Array_combine ($expKey, $expVal));
The function also affects only one layer of the array, and returns a new array
Echo ' <br/>//---------------------------------------------
Array_count_values ($array)
Counts the number of occurrences of each value in the $array array, and takes his value as the key of the new array, with the number of occurrences as value
$array = Array (' v1 ' = ' 265 ', ' v2 ' = ' 352 ', ' v3 ' = ' 265 ', ' v4 ' = ' 349 ', ' v5 ' = ' 265 ');
Printr (Array_count_values ($array));
Summary: This function can only be used to count values of string and integer types, and other types will be warning!
Echo ' <br/>//---------------------------------------------
Array_diff ($array 1, $array 2 ...)
With $array1 as the base array, his value does not appear in any other parameter array to form a new array
$arr 1 = Array (' v4 ' =>458, ' Gren ', ' B5 ', ' A5 ');
$arr 2 = Array (' v4 ' =>598, ' red ', ' A5 ', ' C4 ');
Printr (Array_diff ($arr 1, $arr 2));
Summary: Take an array into a bunch of arrays to find the values that are not in the array, and the statistics and data comparisons should be used
Array_intersect ($array, $PARARR, ...)
This function is functionally identical to Array_diff, except that Array_intersect () returns the common data, Array_diff is only the data that exists in $array
//
Echo ' <br/>//---------------------------------------------
ARRAY_DIFF_ASSOC ($array 1, $array 2 ...)
Same as the Array_diff () function, but this will also be compared with key
//
Echo ' <br/>//---------------------------------------------
Array_diff_key
Same Array_diff () function
It's just this one with the $array1 key to find the other parameter array.
//
Echo ' <br/>//---------------------------------------------
ARRAY_DIFF_UASSOC ($arr 1, $parArr ...., callback function)
function with Array_diff (), but requires a user-defined callback function
Not aware of the function
//
Echo ' <br/>//---------------------------------------------
Array_diff_ukey ($arr 1, $parArr ...., callback function)
function with Array_diff_key (), just like ARRAY_DIFF_UASSOC, requires a callback function
//
//
Echo ' <br/>//---------------------------------------------
Array_fill ($startInt, $numInt, $value)
Populating the $value into a new array, the starting position of the index of the new array is determined by the $startint, $numInt controls how many indexes the array generates.
Tip: In addition to $value, $STARTINT, $numInt must be a number, otherwise error
Printr (Array_fill (2, 5, ' value '));
Summary: I don't know what to do with it.
Echo ' <br/>//---------------------------------------------
Array_fill_keys ($arrKeys, $value);
function with the Array_fill () function. But here's a key for the new array, using the $arrkeys "value of an array"
$arrKeys = Array (' n ', ' d ', ' B ', ' C ');
Printr (Array_fill_keys ($arrKeys, ' value '));
Echo ' <br/>//---------------------------------------------
Array_filter ($arr, callback callback function)
Filter function, by judging the value of the $arr array, if the callback callback function returns True, the current key and the value are added to the new array
TIP: The callback function can write a rule to filter out the array keys that do not conform to the rules.
Function CB ($VAL)
{
return $val%2 = = 0;
}
$array = Array (' K1 ' =>3, ' K2 ' =>5, ' K4 ' =>54654, ' K5 ' =>8794, 8945, 32549564);
Printr ($array, ' CB ');
Tip: callback function names are suggested in quotation marks
Summary: This method can be integrated into a data filter
Unset ($array);
Echo ' <br/>//---------------------------------------------
Array_flip ($array)
Converts the relationship between key and value in the array. Only String and Integr type keys are supported, and other types will issue a warning, and the problematic key value is not converted. In the generated new array, if the key is the same, he will constantly replace the value of the existing key
$arr = Array (' K1 ' = ' v1 ', ' k2 ' = ' v2 ', ' k3 ' = ' v4 ', ' K4 ' = ' v4 ', ' K5 ' = ' v5 ');
Printr (Array_flip ($arr));
Unset ($arr);
Echo ' <br/>//---------------------------------------------
Array_key_exists ($key, $array)
Determines whether a key exists in the current array and returns a bool. can also be used to judge objects
$array = Array (' cb ' = = 234, ' dv ' =>45, ' one ' =>897);
if (array_key_exists (' One ', $array))
Echo ' exists in this array ';
Else
Echo ' does not exist ';
Echo ' <br/>//---------------------------------------------
Array_keys ($array, $selSearch _value)
Returns the key names in the array and makes up a new array, and if the $selsearch_value value is specified, the key name in the array equal to $selsearch_value is returned.
$array = Getarr (4, 10);
Printr (Array_keys ($array));
Printr (Array_keys ($array, ' 5 '));//Search with values
Unset ($array);
Summary: This can also be used for data statistics, data comparison verification
Echo ' <br/>//---------------------------------------------
Echo ' Array_map: ';
Array_map (' CallBack ', $array,...)
Returns the returned value of the callback callback function by returning the passed function
The callback function can also return an array. Also, the callback function only accepts values in an array to pass in
function MAPCB ($n)
{
return $n * $n * $n;
}
$array = Getarr (4, 15);
Printr (Array_map (' MAPCB ', $array));
Echo ' <br/>//---------------------------------------------
Array_merge ($array, $array 2 ...)
Make multiple arrays into an array and rewrite the numeric indexes.
$arr 1 = Getarr (1, 5);
$arr 2 = Getarr (5, 10);
Printr (Array_merge ($arr 1, $arr 2));
Summary: Make multiple arrays into a new array.
Echo ' <br/>//---------------------------------------------
Array_merge_recursive ($arr 1, $arr 2 ...)
function Ibid. Instead, the function makes a new array of values with the same key name instead of replacing the
But if you want it, use it according to the actual situation
Echo ' <br/>//---------------------------------------------
Array_multisort ()
Multidimensional array sorting, currently only two-dimensional array sorting is implemented. Three-dimensional estimation cannot be ranked
This function directly alters the order of the array of members
Echo ' <br/>//---------------------------------------------
Array_pad ($arr, $size, $value)
The array is populated, and if the current $arr is less than $size, the $arr arrays are populated with $value until the $arr is equal to $size
If the length of the $arr is greater than or equal to $size, the function will not populate the $arr. $size less than 0 fills on the left side of $arr, greater than 0 on the right
Echo ' <br/>//---------------------------------------------
Array_pop ($array)
Removes the last key of the array.
Echo ' <br/>//---------------------------------------------
Array_product ($arr)
Returns the product of all the values in an array.
Tip: This function cannot handle data of non-numeric types. If the incoming array contains ' A, b strings ', then PHP will error
$arr = Array (4,5,5);
echo array_product ($arr);
Echo ' <br/>//---------------------------------------------
Array_push ($arr, $KEYARR)
Add $keyarr to the end of the $arr array and add it as a key/stack.
Differences from the Array_merge (), array_merge_recursive () functions:
Arrap_push () adds a $keyarr to $arr, while the other two functions concatenate multiple functions into a single function
Echo ' <br/>//---------------------------------------------
Array_rand ($arr, $num =1)
Take out the keys in the current array, take out a few by $num, default to 1
If $num is 1, then it returns a string
If $num>1 && $num <count ($arr) function returns an array
otherwise PHP error
$arr = Getarr (5, 15);
Printr (Array_rand ($arr, 4));
Echo ' <br/>//---------------------------------------------
Array_reduce ()
Similar to Array_map (), the callback function, the value in the array is processed, and the return value is accepted
The function returns a string. He computes all the values in the array and returns the computed value, while Array_map calculates the value under each key and returns an array
Not too clear, the example read the manual
Echo ' <br/>//---------------------------------------------
Array_replace ($array, $PARARR,...)
Replace the value of the same key in the $array with the value of the key in the parameter array
If the corresponding key is not found in the array of arguments in the $array array, it is added behind the new array
/* $arr = Getarr (4, 10);
$arr 2 = Getarr (6, 15);
Printr ($arr);
Printr ($arr 2); * *
$base = Array (' citrus ' = = Array ("Orange"), ' berries ' = = Array ("BlackBerry", "Raspberry"),);
$replacements = Array (' citrus ' = = Array (' pineapple '), ' berries ' = = Array (' blueberry '));
Printr (Array_replace ($base, $replacements));
Echo ' <br/>//---------------------------------------------
Array_replace_recursive () recursive substitution
function is the same as Array_replace (). The difference is that array_replace_recursive () can operate on a multidimensional array without changing the structure of the $array, and Array_replace () will eventually return a one-dimensional array
$base = Array (' citrus ' = = Array ("Orange"), ' berries ' = = Array ("BlackBerry", "Raspberry"),);
$replacements = Array (' citrus ' = = Array (' pineapple '), ' berries ' = = Array (' blueberry '));
Printr (Array_replace_recursive ($base, $replacements));
Echo ' <br/>//---------------------------------------------
Array_reverse ($arr)
Sort the keys in the array in reverse order
Echo ' <br/>//---------------------------------------------
Array_search ($value, $array)
In the $array array, find the key name with the value $value
Return FALSE if not found
If the $array array hung has more than one $value, then only the first matching key is returned
This function is similar to Array_keys (), except that the return value: Array_search () only returns a matching key name, and Array_keys () can return a one-dimensional array of all matching keys
Echo ' <br/>//---------------------------------------------
Array_shift ($arr)
Removes the first key from the current $arr array and re-orchestrates the number index behind (but does not change the original order), and the non-numeric index is unchanged.
This function is similar to Array_pop (), except that Array_pop () removes the last one, Array_shift () removes the head
Echo ' <br/>//---------------------------------------------
Array_slice ($arr, $offset, $length =0, false) array intercept
Returns the current $arr array starting at offset from $offset, a total of $length elements/keys, and a new array is returned
If the $offset or $length is negative, then it is offset in the opposite direction.
Feel and substring () string intercept similar
Just use the example from the PHP manual.
$input = Array ("A", "B", "C", "D", "E");
$output = Array_slice ($input, 2); Returns "C", "D", and "E"
$output = Array_slice ($input,-2, 1); Returns "D"
$output = Array_slice ($input, 0, 3); Returns "A", "B", and "C"
Note the differences in the array keys
Printr (Array_slice ($input, 2,-1));
Printr (Array_slice ($input, 2,-1, true));
Echo ' <br/>//---------------------------------------------
Array_spslice ($arr, $offset, $length)
In contrast to the array_slice () function, the function removes these elements between $offset and $length
Unset ($arr);
Echo ' <br/>//---------------------------------------------
Array_sum ($arr)
Sums all the values in the $arr array and attempts to convert them if they are non-numeric types, but most of them are converted to 0
This function only affects an array of layers, and array_product () is similar
$arr = Array (
45,56, ' A ', ' B ' =>getarr (1, 2),
);
Printr ($arr);
Echo ' Array_sum ($arr) ', Array_sum ($arr);
Echo ' <br/>//---------------------------------------------
Array_values ($arr)
Extracts the values in the $arr array to form a new array
$arr = Array (
' K1 ' =>45, ' K2 ' =>56, ' k3 ' = ' a ', ' B ' =>getarr (1, 2),
);
Printr (Array_values ($arr));
Echo ' <br/>//---------------------------------------------
Array_unique ($arr) row weight for arrays
The $arr array is drained, and the duplicate values are filtered. Multiple identical values will retain only the first
Echo ' <br/>//---------------------------------------------
Array_walk ($arr, callback[callback function], $sel _perfix= ")
Each key in the current array is sent to the callback function for processing.
If the $sel_perfix parameter is added, the callback function also takes three parameters to receive, otherwise the error
This function affects only one layer
$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Function Test_alter (& $item 1, $key, $prefix)
{
$item 1 = "$prefix: $item 1";
}
Printr (Array_walk ($fruits, ' test_print '));
Array_walk ($fruits, ' test_alter ', ' fruit ');
Echo ' <br/>//---------------------------------------------
Array_walk_recursive ()
function similar to Array_alk (); But he would recursively $arr each layer of array, and the returned array would not change the structure of the original array.
Echo ' <br/>//---------------------------------------------
Arsort ($arr)
Sorting the array by array key name allows you to sort the letters. If the sort fails, NULL is returned
Echo ' <br/>//---------------------------------------------
Asort ()
function is similar to Arsort (), except that: Asort () is a sort of value

The above is the PHP array of the contents of the study notes, more relevant articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.