PHP Arrays, functions

Source: Internet
Author: User
Tags sorts variable scope

Tag: Key value job create pass parameter array element strong tin number Negative

PHP Array:(1) indexed arrays and associative arrays:
(1.) Indexed array: Traditional array, subscript starting from 0. (2.) Associative arrays: Like a dictionary structure, a key value can be a number or a string, and a string equivalent to an integer is treated as an integer, so $arr [3] and $arr[' 3 '] refer to the same element. (3.) inside PHP, all the arrays are stored as associative arrays.
(2) access to array elements:
(1.) $array [' name ']. (quotation marks can be omitted, but to prevent collisions with constant strings, it is best to add)
(2.) $array [0].
(3) storing data in an array:
(1.) If the array does not exist, storing the values in the array creates the array, but retrieving the values in an undefined array does not create the array.
(2.) Use the system's own method: $arr = Array (), the non-pass parameter is an empty array.
(3.) $array = [' Name ' = ' Jack ', age =, gender = ' man '].
(4.)$days = Array (1 = ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri '). (4) Specify the range of values:
(1.)$numbers = Range (2, +), range (10, 5) stores the value backwards. (2.)$letters = Range (' A ', ' Z '). (5) Gets the size of the array:
(1.) Count ()
(2.) sizeof (), is the alias of Count (), no difference
(6) Key and value: (1.) Remove all key values to form a new array:$keys = Array_keys ($array); (2.) Remove all value to form a new array:$values = array_values ($array); (7) Check if the element exists:
(1.)array_key_exists (' name ', $array);
(8) to delete or insert an element in an array: * * * (1.) Array_splice ($array, 2, 3, $addedArray). (9) iterating through an array:
(1.) foreach ($array as $value)
(2.) foreach ($array as $key = $value)
12.php function:(1) define the function:
(1.) function name (parameters) {content}
(2.) The function name is not case-sensitive, for convenience, the inner function is all lowercase
(2) Variable scope:
(1) Function internal variables (function-level scope) and parameters cannot access external functions, and externally defined variables (global scope) can not be interviewedask.
(2.) Global variables: Access by using the Global keyword or $GLOBALS [' a '].
(3.) Static variable: static $a = 0;
(3) function parameters:
(1.) Passing parameters by value
(2.) Pass parameters by reference: function foo (& $num)                      { $num + = +;                      }job 02:1. viewing documents (8) to delete or insert an element in an array: * * * (1.) Array_splice ($array, 2, 3, $addedArray).    The Array_splice () function removes the selected element from the array and replaces it with a new element. The function will also return an array containing the removed elements. Note: If the function does not remove any elements (length=0), it will be removed from the StartThe position of the parameter is inserted into the substituted array (see Example 2). Note: The key names in the replaced array are not preserved. The Array_splice () function, like the Array_slice () function, selects a series of elements in an array, but does not return, but instead deletes them and replaces them with other values. If the fourth argument is supplied, the previously selected elements will be replaced by the array specified by the fourth parameter. The last generated array will be returned.     Array_splice (array, start, [length], [array]) parameter: array: Specify array start: Delete element start position 0: First element positive number: The offset specified by the value in the array begins the removal.     Negative number: The inverse length from the end of the array: optional parameter, numeric value, number of elements removed, and 0 of the returned array: The array does not change positive numbers: Removes the quantity of elements. Negative number: Removes the value from start to the end of the count. All elements are not set: Removes all elements from the start parameter set to the end of the array: an optional parameter that specifies an array with elements to insert in the original array if there is only one element, you can set the String, do not need to be set to function return value: Returns an array of Array_splice ($array 2, 0) consisting of the extracted elements;
Print_r ($array 2); Array () Delete all
Echo ' <br> ';

$array 3 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 3, 0, 2); Delete two elements starting with subscript 0
Print_r ($array 3); //
Echo ' <br> ';
$array 4 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 4, 0,-2); Starting with subscript start0, until the end of the array
Print_r ($array 4); Array ([0] = [1] = 23)
Echo ' <br> ';
$array 5 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 5, 5, 0); Array does not change
Print_r ($array 5); //
Echo ' <br> ';

$array 5 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 5,-5, 2); From the bottom Fifth element, delete the two
Print_r ($array 5); //
Echo ' <br> ';

$array 5 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 5,-5,-2); From the bottom fifth element to the penultimate one, delete the start length
Print_r ($array 5); //
Echo ' <br> ';

$array 5 = Array (12, 45, 32, 26, 15, 54, 45, 23);
$arr = Array (11, 14);
Array_splice ($array 5, 5, 0, $arr); Elements in Arr are added in the subscript Five element except the beginning of the array
Print_r ($array 5); //
Echo ' <br> ';
$array 5 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 5, 5, 1, $arr); Start deleting elements of length 1 at subscript 5 and inserting elements from the ARR array
Print_r ($array 5); //
Echo ' <br> ';

$array 5 = Array (12, 45, 32, 26, 15, 54, 45, 23);
Array_splice ($array 5, 5, 0, ' ASD '); Insert string at subscript 5
Print_r ($array 5); //
Echo ' <br> ';    The 1.sort () function sorts the indexed array in ascending order. Note: This function assigns the new key name to the cells in the array. The original key name is deleted. Sort (array, [Sortingtype]) array: Specifies the arrays to be sorted Sortingtype: Optional parameter, 0: Default-sort_regular usually compares items (does not change type) 1:-Sort_numeric each item Compare as Values 2:-sort_string each item as a string comparison 3:-Sort_locale_string each item as a string to compare, according to the current locale. (You can use setlocale () to change) 4:-Sort_natura L each item as a string to handle, using a natural sort like natsort () 5:-sort_flag_case can be combined (bitwise OR) with sort_string or Sort_ NATURAL sorts strings, not case-sensitive return values: Returns TRUE if successful, otherwise FALSE. The 2.rsort () function sorts the numeric array in descending order.  The Rsort (array, [Sortingtype]) 3.asort () functions are sorted in ascending order of key values, and the value corresponding key does not change the Asort (array, [Sortingtype]) parameter above 4. The Arsort () function sets the array in descending order of key values, and the value corresponding key does not change by 5. An array of Ksort () functions is sorted in ascending order by key name, key ascending 6. Krsort () function in descending order of the key name of the array key descending

PHP Arrays, functions

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.