PHP Knowledge points--arrays

Source: Internet
Author: User

First, the classification of the array
1, associative array: usually refers to the subscript as a string, and the string can generally express the meaning of the data array.
2, index array: usually refers to an array of subscript is a strict starting from 0 consecutive digital subscript-as with the JS array.


Second, from the array level to divide:
One-dimensional arrays:
Is the value of each element in an array, which is a normal value (not an array value)
$arr 1 = Array (
"Name" = "Floret",
"Age" =>18,
"Edu" = "University Graduate",
);

Two-dimensional arrays:
Each item in an array is another one-dimensional array.
$arr 1 = Array (
"Name" = = Array (' Floret ', ' small Fang ', ' Xiao Ming ',);
"Age" =>array (18, 22, 19),
"edu" = = Array ("University graduate", ' Secondary school ', ' Primary school ')
);

Multidimensional Arrays:
etc... ‘
General syntax form for multidimensional arrays:
$v 1 = array name [subscript] [subscript] [...]

Traverse the basic syntax:
foreach ($arr as [$key = =] $value)//$key can be called a key variable, $value can be called a value variable.
{
Here you can do all the possible things for $key and $value-because they are a variable
$key represents every time an index of an element is obtained, possibly a number, or it can be a string
$value represents the value of the element each time it is acquired, possibly various types.
This loop structure iterates through the first item of the array, loops to the last item, and then ends.
}

Three, array pointers and traversal principle
Each array has a "pointer" inside it that determines the element that is taken when the array is currently being evaluated.
The foreach traversal process is dependent on the pointer!
Example: $arr 1 = Array (2=>1, ' DD ' =>5, 1=>1.1, "abc", 0=>true)
The pointer is in addition to the position setting for the Foreach loop, and some other functions also depend on the pointer:
1, $v 1 = current ($arr 1);//Gets the value of the element that is currently pointed to by the pointer in $arr1, or False if no element is pointed to
2, $v 1 = key ($arr 1),//Gets the subscript of the element pointed to by the current pointer in $arr1, ... False
3, $v 1 = next ($arr 1);//move the pointer to "next element" and get the value of the next element;
4, $v 1 = prev ($arr 1);//Move the pointer to the previous element and get the value of the previous element
5, $v 1 = reset ($arr 1);//move the pointer to "first element" and get the value of the element--array pointer class
6, $v 1 = end ($arr 1);//move the pointer to "last element" and get the value of the element
7, $v 1 = each ($arr 1);//Gets the subscript and value of the current element, and then moves the pointer to the next position.

The

For+next+reset iterates through the array
Reset ($arr 1);//Resets the array, that is: array pointer initialization, where the returned data is "discarded".
$len = count ($arr 1);
for ($i = 0; $i < $len $i + +) {
$key = key ($arr 1),//subscript
$value = current ($arr 1);//value
//Then you can do any action on $key and $value as a variable
next ($arr 1 );//Here, move the pointer to the next element (also discards the return value)
}
While+each () +list () iterate through the array
each () function explanation:
each () The function can take the subscript and value of an element in an array, and then put it in a new array, and the pointer moves back one bit.
The new array, which has 4 elements, but stores the subscript and the value of the "double", similar to the following form:
Array (
1 = value taken out,
' value ' = > The value taken out,
0 = > The subscript (Key name), ' key ' and the subscript (key name) to be taken out
);

The

List () function explains:
Usage form:
List ($v 1, $v 2, $v 3, $v 4 .... ) = array $arr1; The
function is: To get the array $arr1 subscript 0,1,2,3, .... The value of the element, and one time into multiple variables (one by one corresponds)
is equivalent to the following statement:
• $v 1 = $arr 1[0];
• • $v 2 = $arr 1[1];
• • $v 3 = $arr 1[2];
• • $v 4 = $arr 1[3];
.....
But note: You can only implement the "value of an element that starts with 0 consecutive numbers" (but not the order of the elements in the array)
and then start using the 2 functions and the while loop structure to implement the array traversal:
Form:
Reset ($ ARR1); The
while (list ($key, $value) = each ($arr 1))//Removes the element from the array $arr1,
//returns false at the end of the array, that is, the loop ends
{
//here, The $key, and the $value can be manipulated.
}
foreach traversal details
foreach is also a normal looping syntax structure that can have break and continue operations. The
pass-through value is passed as a value by default during traversal.
during traversal, value variables can be manually set to reference delivery:
Oforeach ($arr as $key = & $value) {...}
Key variable cannot be set to reference pass
foreach defaults to traversal on the original array. However, if an array is modified during traversal or some kind of pointer operation (that is, the preceding pointer function), the arrays are copied and the loops continue to be traversed on the copied array.

Bubble Sort:
Target: Arrange the following arrays in a positive order (small to large)
$arr 2 = Array (5, 15, 3, 4, 9, 11);
General Logic Description:
1, for the array starting from the first element, from left to right, the next 2 elements of the comparison size: If the left side is larger than the right, then the two of them swap positions, the result:
Array (5, 15, 3, 4, 9, 11); (original)
Array (5, 15, 3, 4, 9, 11);
Array (5, 3, 15, 4, 9, 11);
Array (5, 3, 4, 15, 9, 11);
Array (5, 3, 4, 9, 15, 11);
Array (5, 3, 4, 9, 11, 15);
At this time, only "go through a cycle", continue the next round:
Array (5, 3, 4, 9, 11, 15); (initial)
Array (3 5, 4, 9, 11, 15);
Array (3 4, 5 9, 11, 15);
Array (3 4, 5 9, 11, 15);
Array (3 4, 5 9, 11, 15);
Continue to the next round:
Array (3 4, 5 9, 11, 15);
Implied logical description (assuming that the array has n entries):
1, the "bubbling" comparison process of n-1 is required.
2, each trip compared to the previous trip less than once, the first trip needs to compare n-1 times
3, each comparison, is from the beginning of the array (0), with the close to the element comparison, and exchange (when needed)

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <title>Bubble sort</title>6 </Head>7 8 <Body>9 <?PHPTen $arr 2 = array (5,15,3,4,9,11); One $len = count ($arr 2); The count () function is used to count the number of array elements, the count () function is count ($array, mode), and mode is an optional parameter A //A bubbling comparison that requires a n-1 trip - echo "<br/> before sorting:"; - Var_dump ($arr 2); Print_r are typically used to print information about variables, which are typically used in debugging.  the For ($i =0; $i < $len-1; $i + +) { - For ($k =0; $k < $len-$i; $k + +) { - if ($arr 2[$k]> $arr 2[$k +1]) { - $temp = $arr 2[$k]; + $arr 2[$k] = $arr 2[$k +1];  - $arr 2[$k +1]= $temp; +         } A     } at } - echo "<br/> after sorting:"; - Var_dump ($arr 2); -     ?> - </Body> - </HTML>
View Code

Select sort
Target: Arrange the following arrays in a positive order (small to large)
$arr 2 = Array (5, 15, 3, 4, 9, 11);
General Logic Description:
1th trip: Obtain the maximum value in the array and its subscript, and then "swap" with the last item of the array (the bottom 1th is determined)
2nd trip: Obtain the maximum value in the array except the last 1 items and their subscript, and then exchange with the reciprocal 2nd item (the countdown is determined)
3rd trip: Obtain the maximum value in the array except the last 2 items and their subscript, and then exchange with the reciprocal 3rd item (the countdown is determined)
Implied logical description (assuming that the array has n entries):
1, it is possible to conclude a n-1 trip.
2, each trip to find the number of data than the previous trip less one, the 1th trip to find n
3, each time you find the maximum value of the item, and the item to be exchanged with the position, minus 1, the first position n-1

PHP Knowledge points--arrays

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.