PHP Learning Notes array values and array traversal and sequencing _php tutorial

Source: Internet
Author: User
Tags php introduction
This article to introduce a simple example of PHP introduction, here is mainly about the PHP array value operation and function and array traversal and array sorting examples, you can enter the reference.

Operation of Array values

1. Extraction of values
In PHP, a list is used to extract the values in the array, such as list ($a, $b) = $array. If the value in the list is more than the number of arrays, the extra value in list is set to NULL. You can also use commas to jump through values in the array, such as list ($a, $b) = $array.

2. Dividing an array
If you want to get a subarray, you can use Array_slice (array, offset, length) to get. It returns an array with a new subscript starting at 0. If the index of the original array is a string, it seems that there is no meaning, it is best not to use, you can use Array_splice to get the substring.

3. Dividing the array into multiple arrays
Use Array_chunk to divide an array into a two-dimensional array. Detailed information can be seen through the official description of the link.

4. Keys and values
Array_keys ($array), gets an array of array indexes
Array_value ($array), gets an array of array values, and the index is reassigned starting from 0.
Array_key_exists ($key, array), whether the element exists for checking.
Array_splice, delete the insertion element.

5. Conversions between arrays and variables
Extract (array) to change an array into a variable
Compact () turns the variable into an array

6. Finding the array
In_array (array,) returns whether the element exists in the array.
Array_search () returns the index of the element being found.

7. Entire Array function
The sum of the Array_ sums () is computed for the array.
Array_ merge () merges two arrays.
Array_ diff () a different value between two arrays.
Array_ filter () filtering element

8. Collections, Stacks, queues
Array_ unique () takes a collection of two arrays, preserving the index of the previous array if the values are the same.
Array_ intersect () takes a two-digit intersection, preserving the index of the first array.
Array_ push () joins the stack.
Array_ pops () pops up the stack.
Array_ shift () joins the queue.
Array_ unshift () popup queue.


1. Simple traversal
In PHP, the simplest way to traverse an array is for and foreach. Where foreach is written in two ways, one that only iterates through the values, and the other iterates through the indexes and values. The following code can be seen in detail.

copy code
$test = Array (' A ', ' B ', ' C ');
/For
for ($i = 0; $i < count ($test), $i + +) {
echo $test 01[$i];
}
//foreach value only
foreach ($test $value) {
echo $value;
}
//foreach key and value
$test = Array (' a ' = = ' aaaa ', ' b ' = ' bbbb ', ' c ' = ' cccc ');
foreach ($test $key = + $value) {
echo $key = = $value ";
}

2. Iterator traversal
in PHP, the following functions are used primarily for iterative traversal. The current element of the
present () iteration.
Reset () moves back to the first element and returns it.
Next () moves to the next element and returns it.
Prev () moves to the previous element and returns it.
End () moves to the last element and returns it. The
each () returns the index and value of the current element as an array and moves to the next iteration.
Key () returns the current index.
Array_ Walk () invokes a function for each element.
Array_ reduce () is calculated for each element in turn.

copy code


$test = Array (' a ' = = ' aaaa ', ' b ' + = ' bbbb ', ' c ' = ' cccc ');
while (list ($key, $value) = each ($test)) {
echo "$key = = $value". "N";
}
Array_walk ($test, walk_test);
function Walk_test ($key, $value) {
echo "walk: $key = = $value". "N";
}

$test = Array (1, 2, 3, 4, 5);
Echo array_reduce ($test, reduce_test);
Function Reduce_test ($run _result, $current _value) {
return $run _result + $current _value * $current _value;
}


3. Sorting of arrays


In PHP, there are three sorting methods, sorted by index, sorted by value (not preserving the original index), sorted by value (preserving the original index). Each of these is divided into ascending, descending, and user-defined order of three functions. They were as follows:
Sort by index: ① ascending ksort () ② descending krsort () ③ user-defined order Uksort ()
Do not retain the original index value sort: ① ascending sort () ② descending rsort () ③ user-defined order Usort ()
Preserve the original index value sort: ① ascending asort () ② descending arsort () ③ user-defined order Uasort ()
In PHP, you can also use Array_multisort to sort multiple arrays at once, but you might use less in your project.
Flips the array, flips the number index, and the index starts again from 0: Array_reverse ()
Swap index and value: Array_flip ()
Random order: Shuffle ()

http://www.bkjia.com/PHPjc/628634.html www.bkjia.com true http://www.bkjia.com/PHPjc/628634.html techarticle This article to introduce a simple example of PHP introduction, here is mainly about the PHP array value operation and function and array traversal and array sorting examples, you can enter the reference ...

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