Commonly used in PHP array functions ____ Functions

Source: Internet
Author: User

To create an indexed array

$arr =array (' name ' => ' Molaifeng ', ' age ' => ', ' height ' => ' 167 ');

foreach ($arr as $value) {

echo $value. ' ';

}

echo '

when referencing an array cell indexed by a character, the index must be enclosed in single quotes

echo $arr [' name '], '


to determine whether an array is empty

$arr = Array ();

echo Empty ($arr)? ' The array is empty

change the values in the array by foreach

$arr = Array (1,2,3,4,5,6);

foreach ($arr as $index => $value) {// This method is especially useful for automatically generating update statements

echo $arr [$index] = $value *2;

Echo ';

}

echo '

to determine whether an index is within the array

$arr =array (' name ' =>molaifeng, ' age ' => ', ' height ' => ' 167 ');

Echo array_key_exists (' age ', $arr)? ' Existence

determines whether the value is within the array

echo In_array (' $arr ')? Existence of

keys (index) and value of the set of print sets

Print_r (Array_keys ($arr));

echo ' <br/> ';

Print_r (Array_values ($arr));

echo '

computes the length of the array, returns 1 for other types, and returns 0 for null.

Echo ' The length of the array is: ', count ($arr), ' <br/> ';

Echo ' Count is not an array but a string ', count (' string '), ' <br/> ';

Echo ' Count is not an array but empty ', count (null), '

function of a cell that points to an array by pointer Current,next,prev,end,reset,key

Echo Prints the current value, that is, the index is 0 or the first value of the array: ', $arr, ' <br/> ';

Echo ' Prints the next value: ', Next ($arr), ' <br/> ';

Echo ' Prints the last value: ', End ($arr), ' <br/> ';

Echo ' Prints the previous value: ', prev ($arr), ' <br/> ';

echo ' Reset: ', reset ($arr), ' <br/> ';

Echo ' Displays the current index: ', Key ($arr), '

List Function , first the internal variables of the list and the cells in the array

whether or not the element in the array is in order, from array[0], each corresponding.

one by one corresponds, assign from right to left.

List ($a, $b) =array (' name ', ' Molaifeng ')

echo $a, ', $b, '

Each function is to read the current key/value and put it in an array and move the inner pointer of the array to the tail.

Echo returns an array ([1] => Molaifeng [value] => Molaifeng [0] => name [key] => name)

$tmp = each ($arr);

Print_r ($TMP);

echo ' <br/> ', $a, ', $b, '

Echo ' <span style= "color:red" > Combined list and each simulate foreach</span><br/> ';

$arr =array (' name ' => ' Molaifeng ', ' age ' => ', ' height ' => ' 167 ');

while ($temp = each ($arr)) {

List ($key, $value) = $temp;

Echo $key, '--', $value, ' <br/> ';

}

echo '

keys and values in the Array_flip Exchange array
Print_r (Array_flip ($arr));

echo '

Range (low,hight), which prints from low to high or from high to low

Print_r (range (1,10));

echo ' <br/> ';

Print_r (range (10,1));

echo '

combine range and array_sum to calculate values from 1 to 100

Echo array_sum (range (1,100)), ' <br/> ';

$arr = Array (' A ', ' B ', 1,2,3);

echo array_sum ($arr), '

Array_push, append the unit to the array, press one or more units into the end of the array (into the stack), recommend the Direct Action array to append, as follows

$arr =array (' name ' => ' Molaifeng ', ' age ' => ', ' height ' => ' 167 ');

Array_push ($arr, ' weight ', ' hobby ');

$arr [] = ' new '; Remember, it's very useful.

Print_r ($arr);

echo '

array_pop-The last cell of the array (out of stack)

Array_pop ($arr);

Print_r ($arr);

echo '

array_shift-moves the cell at the beginning of the array to the group (out of the stack) and returns the value of the cell
echo Array_shift ($arr), ' <br/> ';

Print_r ($arr);

echo '

array_unshift-inserts one or more cells at the beginning of the array (into the stack)

All numeric key names are modified to be counted from zero and all the text keys remain unchanged.

Array_unshift ($arr, ' Molaifeng ');

Print_r ($arr);

echo '

Sort the array, sort the array elements from small to large, and rearrange the keys, starting at 0

$arr = Array (9,11,1,2,3,4);//associative array sort

Sort ($arr);

Print_r ($arr);

echo ' <br/> ';

$arr = Array (' A ' =>13, ' B ' =>2, ' C ' =>1, ' d ' =>9, ' d ' =>5, ' e ' =>0);

Sort ($arr);//associative array sorting, but missing character index

Print_r ($arr);

echo ' <br/> ';

$arr = Array (' A ' =>13, ' B ' =>2, ' C ' =>1, ' d ' =>9, ' d ' =>5, ' e ' =>0);

Asort ($arr);//associative array sorting, preserving character index

Print_r ($arr);

echo '

combine the two numbers and

$arr 1 = array (9,11,1,2,3,4);

$arr 2 = Array (' A ' =>13, ' B ' =>2, ' C ' =>1, ' d ' =>9, ' d ' =>5, ' e ' =>0);

Print_r (Array_merge ($arr 1, $arr 2));

echo '

to find the intersection of the arrays

$arr 1 = array (9,11,1,2,3,4);

$arr 2 = array (1,2,3,4,5);

Print_r (Array_intersect ($arr 1, $arr 2));

echo '

array_unique-the duplicate values in the divisor group

$arr = Array (9,11,1,2,3,9,2,3,4);

Print_r (Array_unique ($arr));

echo '

array_reverse-Returns an array of Print_r (Array_reverse ($arr) in reverse order;

echo '

manually write a method about the reverse of an array

$arr = Array (1,2,3,4,5);

Function Fan ($arr) {

$temp = Array ();

foreach ($arr as $v) {

Array_unshift ($temp, $v);

&nb

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.