PHP Learning Array Courseware

Source: Internet
Author: User
Tags echo date
Subscript: The identifying name in the array is the code of the string or integer in the array

Several index values in an array are referred to as several-dimensional arrays.
Index value: An index is a structure that sorts the values of one or more columns in a database table.

Array classification
The PHP array is divided into two types:
Indexed array: index (indexed) index value is an integer, starting at 0, with an indexed array when the object is identified by its position.
Associative Arrays: Association (associative) associations are indexed by string values, indexed values are column names, and the term accesses column data.

Arrays are usually assigned in the same way
In general, there are two ways to assign a value to an array:
$a [1]= "DSADSADSA";
$b [2]= "Dsadsadsad";
Use the array function:
$a =array ("Dsads", "DSADSA", 321312);
One-dimensional array: The index value (subscript) of an array is called a one-dimensional array only one time.
The format of the array's direct assignment:
$ array variable name [index value]= data content;
Note: The index value can be either a string or an integer but 1 differs from "1" in that they are one of the integers that belong to the string.

Arrays with the same name that do not give index values are ordered in order.
Instance:
<?php
$a =array (1,2,3,4,5,6);
$b =array ("One", "one", "one", "three");
$c =array (0=> "AAA",1=> "BBB",2=> "CCC");
$d =array ("AAA",6=> "BBB", "CCC");
$e =array ("name" = "Zhang", "Age" =>20);
?>
Two-dimensional arrays
Format of multidimensional arrays:
$a [0][]= "Dsadas";
$a [0][]= "DSADSA"; This group is $ A 0 under the index value of 1 and 2
If you use the array function to declare the format as follows:
$a =array ("Dsadsa", "Dsadas", 21,array ("Dsadsa", "Dsadas"));

Traversal of an array
foreach Loop structure:
foreach is used only in the form of loops with arrays
foreach (Array_exprssion (array representation) as $value);
foreach (array_exprssion (array expression) as $key = = $value);
The first format iterates over the given array of array_exprssion. Each time the current value in the loop is assigned to me $calue, and the pointer inside the array moves forward one step.
The second format does the same thing, except that the key value of the current cell is assigned to the variable $key in each loop.
When foreach starts executing, the pointer inside the array automatically points to the first cell. Also note that foreach is manipulating a copy of the specified array, not the array itself
$arr =array (10,20,30,40,50,60);
foreach ($arr as $k = = $v) {
echo "$k = <br> $v";
}

Output result: 0=>10 1=>20 2=>30 3=>40 4=>50 5=>60//subscript = = Integer
Union using list (), each (), and while loop
each ():
$arr =array (1,2,3,4,5);
$a =each ($arr);
Print_r ($a);
Output: Array ([1] = 1 [value] = 1 [0] = 0 [key] + 0)
Takes the value of the first bit of the array as the index key
List ():
$arr 3=array ("A", "B", "C");
List ($key, $value) =each ($arr 3);

echo $key. " <br> ". $value;
Output: 0 A list () can be said to be a one-step operation to a set of variables can only be used for the numeric index of the array and assume that the numeric index starting from 0.
While loop
$arr =array (1,2,3,4,5,6,7,8,9,);
while (list ($key, $value) =each ($arr)) {
$key + +;
echo $key. " = ". $value;
echo "<br>";
}
echo "<br>";
Output: 1=>1 2=>2 3=>3 4=>4 5=>5 6=>6 7=>7 8=>8 9=>9
Reset () array pointer redirection
After each (), the array pointer stays in the next cell in the array, or when the end of the array is encountered, the last cell.
Is_array detects if a variable is an array true returns Ture false false
$arr =array (1,2,3,4,5,6, "SaaS");
while (list ($k, $v) = each ($arr))
{
if (Is_array ($arr))
{
$x + = $v;
echo $x;
}
Else
{
$x + = $k;
}
}
This example does not fully reflect the functionality of Is_array, but can be consulted.
Pointers to arrays
Next (): Responsible for moving the pointer backwards
Prve (): Responsible for moving the pointer forward
End (): points the pointer to the last element of the array
Reset (): Moves the current pointer unconditionally to the first index position
Syntax format: Mixed next (array name)
$arr = (Array (1,2,3,4,5));
Echo End ($arr);
Output results: 5
Key () and current () and count ()
The function of key () is to read the index value of the data pointed to by the current pointer.
The current () function reads the contents of the data pointed to by the pointer.
The function of Count () is used to calculate the number of all elements in the array, which means that the function returns the length value of the target array.
Format: int count (array name);

Key (): Gets the key name from the associative array
$array = Array (' fruit1 ' = ' apple ', ' fruit2 ' = ' orange ', ' fruit3 ' = ' grape ', ' fruit4 ' = ' apple ', ' fruit5 ' =&gt ; ' Apple ');
while ($fruit _name = current ($array)) {
if ($fruit _name = = ' Apple ') {
echo Key ($array). ' <br/> ';
}
Next ($array);
}
Output Result: fruit1,fruit4,fruit5

Current (): Returns the cell in the array
$transport = Array (' foot ', ' bike ', ' car ', ' plane ');
$mode = current ($transport); $mode = ' foot ';
$mode = Next ($transport); $mode = ' bike ';
$mode = current ($transport); $mode = ' bike ';
$mode = prev ($transport); $mode = ' foot ';
$mode = End ($transport); $mode = ' plane ';
$mode = current ($transport); $mode = ' plane ';
Note that the sample returns the current cell in the array

COUNT (): Calculates the number of cells in the array
$arr =array (1,2,3,4,5,6);
echo count ($arr);
Output results: 6

Array_change_key_case ()
Array_change_key_case returns an array of string key names that are all lowercase or uppercase
The morphological functions included are two [Case_upper] converted to uppercase, [cas_lower] converted to lowercase.
$input _array = Array ("First" = 1, "SecOnd" = 4);
Print_r (Array_change_key_case ($input _array, case_upper));
Output: Array ([First] = 1 [SECOND] = 4)

Array_chunk ()
The Array_chunk () function takes the data contents of the target array to the specified number of indexes, and breaks it down into a few small arrays contained in the original array.
$arr =array (1,2,3,4,5,6);
$a =array_chunk ($arr, 3);
Print_r ($a);
Output: Array ([0] = = Array ([0] = 1 [1] = 2 [2] = 3) [1] = = Array ([0] = 4 [1] = 5 [2] = 6 ) )
That's equal to dividing the sum of the number of array cells by 3.

Array_count_values
Array_count_values used to calculate the number of occurrences of each value in the target array
Syntax format: array_count_values (destination array)
The result value returned by this function is indexed by the contents of the original array and represented in the form of the array.
$arr =array (1,2,3,3,2,6);
Print_r (Array_count_values ($arr));
Output: Array ([1] = 1 [2] = 2 [3] = 2 [6] = 1)

Array_fill
Array_fill allows the user to customize the values to fill the specified index segment in the target array.
Syntax format: Array_fill (starting index position, section size, specified characters)
Where the starting index position and the segment size must be an integer (integer) morphological value, and the segment size cannot be specified as 0.
$a = Array_fill (5, 6, ' banana ');
Print_r ($a);
Output: Array ([5] = banana[6] = banana[7] = banana[8] = banana[9] = banana[10] = banana)

Array_filter
Array_filter each value in the target array in turn to the consumer custom function. If the consumer custom function returns True (True
), the current value of the input array is included in the returned result array. The key name of the array remains unchanged.
Syntax format: array_filter (target array, consumer custom function)
function Odd ($var)
{
Return ($var% 2 = = 1);
}
$array 1 = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
Print_r (Array_filter ($array 1, "odd"));
Output: Odd:array ([a] = 1[c] [3[e] = 5)
$entry = Array (0 = ' foo ', 1 = false,2 = -1,3 = null,4 = ");
Print_r (Array_filter ($entry));
Output result Array ([0] =>foo[2] +-1)
Other callback functions: Array_walk (), Array_map ()

Array_flip
Array_flip the index in the target array with the content value, the inverted array is indexed as the original content value, and the index is used as the data content.
Array_flip syntax format: Exchanging keys and values in an array
Array_flip (target array)
$trans = Array ("a" = = 1, "b" = 1, "c" = 2);
$trans = Array_flip ($trans);
Print_r ($trans);
Output content: Array ([1] =>b[2] =>c)
If the same value occurs more than once, the last key will be its value, and all the others are lost.

Array_sum
The array_sum is used to calculate the sum of all the element values in the array.
Syntax format: array_sum (destination array)

Array_unique
The array_unique is used to repeat the data in the object array, and the new value is returned after the operation.

Array_keys and Array_values
Array_keys the function ignores the original key name and uses the sequential numeric array to re-index the associative array.
Array_values the function returns a key an array group that contains a number or string. Returns all the values in the input array and establishes a numeric index on them.
$arr =array ("A" =>1, "B" =>2, "abc", 2);
$b =array_keys ($arr);
$c =array_values ($arr);
Output: $b =array ([0] = a [1] = b [2] = 0 [3] = 1)
$c =array ([0] = 1 [1] = 2 [2] = ABC [3] = 2)

Retrieving the array
The retrieval of an array is mainly determined by the key name or the element value of the logarithm group, thus determining the existence or otherwise of an element of the set.
Syntax format: BOOL In_array (mixed $needle, array $haystack [, BOOL $strict]);
$arr 3=array ("Hello", "Wprld");
$b =in_array ("Hell", $arr 3,true);
Var_dump ($b);
$arr 4=array (1,2,3,array ("Hello", "World");
$b =in_array (Array ("Hello", "World"), $arr 4);
Var_dump ($b);
Output Result: BOOL (FALSE) bool (true)

Converting between arrays and variables
The extract () and compact () functions convert between arrays and variables.
After conversion, the key name of the array element and the variable name, as well as the value of the element, maintain a corresponding relationship with the value of the variable.
Extract this function to pour a variable from the array into the current symbol table
$info = Array ("user_id" =>2006001, "user_name" = "Tong Xiang Yu", "Position" and "project manager");
Extract ($info);
echo $user _id;
Output results: 2006001
The compact () accepts a variable number of parameters. Each parameter can be a string that includes the variable name or an array containing the variable name, which can also contain an array of other cell contents as variable names, and the compact () can be recursively processed.
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location _vars = Array ("City", "state");
$result = Compact ("event", "Nothing_here", $location _vars);
Print_r ($result);
Output: Array ([Event] = SIGGRAPH [City] = San Francisco [state] = = CA)

Arrays and stacks
The Array_push () function presses one or more elements into the end of the array stack (into the stack), returning the number of elements in the stack.
The Array_pop () function pops the last element of the array stack and returns the element.
Example Array_push
$a =array ("DSA", "DSAs", "Dfs", "GFD");
Array_push ($a, "ssss", "ddddd");
Print_r ($a);
Output: Array ([0] = + DSA [1] = DSAs [2] = = DFS [3] = GFD [4] = = SSSS [5] = ddddd)
The example Array_pop pops up and returns the last cell of the array, minus one of the array's length. If array is empty (or not an array), NULL is returned.
$a =array ("DSA", "DSAs", "Dfs", "GFD");
Array_pop ($a);
Print_r ($a);
Output: Array ([0] = + DSA [1] = = DSAs [2] = = DFS)

Arrays and pairs of columns
The queue can also appear in the form of an array, the first element in the array is the opponent, and the last is the tail.
You can use the stack method on the tail insert element.
Removing the head element from the queue array can use the Array_shift function, which moves the first element of the array out and returns as a result, while the array length is reduced by 1, and the other elements move forward one bit. All numeric key names are changed from zero to counting, and the text key name does not change.
Array_shift Example
$a =array ("DSA", "DSAs", "Dfs", "GFD");
$title =array_shift ($a);
Print_r ($a);
Output: Array ([0] = DSAs [1] = = DFS [2] = = GFD)
You can use the Array_unshift () function to insert one or more elements at the beginning of an array of queues, which returns the number of successfully inserted elements.
$a =array ("DSA", "DSAs", "Dfs", "GFD");
$title =array_unshift ($a, "a", array);
Print_r ($a);
Output: Array ([0] = a [1] = Array ([0] = 1 [1] = 2) [2] = + DSA [3] = = DSAs [4] + = DFS [5] = = GFD)
The result shows that you can also insert an array

Sorting of arrays
Array ordering for ignoring key names
Sort () is
Rsort () pour
Usort ()
$arr = Array ("3" = "lemon", "2" = "Orange", "4" = "Banana", "1" and "Apple");
Ksort ($arr);
foreach ($arr as $key = = $val) {
echo "$key = $val \ n";
}

Calculation of arrays
The calculation of an array is convenient. The simplest calculation is to find the sum of all the elements inside the array. The array can also be processed as a set, merging two or more arrays, and calculating the difference or intersection between them.
Merge merging of arrays is not a +
Example
$arr 2=array (a);
$arr 3=array (3,4,5);
$a =array_merge ($arr 2, $arr 3);
Print_r ($a);
Output: Array ([0] = 1 [1] = 2 [2] = 3 [3] = 3 [4] = 4 [5] = 5)
Calculating the difference set of an array
$array 1 = Array ("A" = "green", "Red", "Blue", "Red");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);
Print_r ($result);
Output: Array ([1] = blue) difference is taken from the previous difference value
Computes the intersection of an array
$array 1 = Array ("A" = "green", "Red", "blue");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
Print_r ($result);
Output: Array ([a] = green [0] = + red) intersection take the upper and lower sides together

To create an array of the specified range:
Range ();
To move a duplicate value in an array:
Array_unique ();
Returns an array in reverse order:
Array_reverse ();

Random function of array: random number
Srand (float) microtime () *10000000) set random generator seed
Array_rand ();
Array_shuffle ();
Example
echo Date ("YMD"). Rand (1000000,9999999). " JPG ";

Output result: 200805096246795jpg of the number of immediately

The above is the PHP learning array courseware content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.