Photoshop Get started learning PHP Learning Array Courseware 1th/2 page

Source: Internet
Author: User
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:
$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 and $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. "
". $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 "
";
}
echo "
";
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). '
';
}
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)

Current 1/2 Page 12 next page

The above introduces the introduction of Photoshop Learning PHP Learning Array Courseware 1th/2 page, including the introduction of Photoshop learning aspects, I hope to be interested in PHP tutorial friends helpful.

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