Php array character conversion and sorting (php Tutorial 2)

Source: Internet
Author: User
Tags arrays numeric value sorts

Introduction to php Tutorial array
The array function allows you to operate on arrays.

Php supports single-dimension and multi-dimensional arrays. At the same time, the database query results are provided to construct an array function.
Install
Array functions are the core component of php. These functions can be used without installation.
Php array functions
Php: indicates the earliest php version that supports this function.

Function description php
Array () to create an array. 3
Array_change_key_case () returns an array with uppercase or lowercase keys. 4
Array_chunk () splits an array into new array blocks. 4
Array_combine () creates a new array by combining two arrays. 5
Array_count_values () is used to count the number of occurrences of all values in the array. 4
Array_diff () returns the number of difference sets of two arrays. 4
Array_diff_assoc () compares the key name and key value, and returns the number of difference sets of the two arrays. 4
Array_diff_key () compares the key names and returns the number of difference sets of the two arrays. 5
Array_diff_uassoc () performs an index check using the callback function provided by the user to calculate the array's difference set. 5
Array_diff_ukey () uses the callback function to compare the differences of the calculated array with the key name. 5
Array_fill () fills the array with the given value. 4
Array_filter () uses the callback function to filter elements in the array. 4
Array_flip () exchanges the keys and values in the array. 4
Array_intersect () calculates the intersection of arrays. 4
Array_intersect_assoc () compares the key name and key value, and returns the intersection array of the two arrays. 4
Array_intersect_key () uses the key name to compare and calculate the intersection of arrays. 5
Array_intersect_uassoc () carries an index check to calculate the intersection of arrays and compares indexes with callback functions. 5
Array_intersect_ukey () uses the callback function to compare key names to calculate the intersection of arrays. 5
Array_key_exists () checks whether the given key name or index exists in the array. 4
Array_keys () returns all the key names in the array. 4
Array_map () applies the callback function to the units of the given array. 4
Array_merge () combines one or more numbers into an array. 4
Array_merge_recursive () recursively merges one or more arrays. 4
Array_multisort () sorts multiple arrays or multi-dimensional arrays. 4
Array_pad () fills the array with a value to the specified length. 4
Array_pop () pops up the last unit of the array (output stack ). 4
Array_product () calculates the product of all values in the array. 5
Array_push () pushes one or more units (elements) to the end of the array (inbound stack ). 4
Array_rand () randomly selects one or more elements from the array and returns them. 4
Array_reduce () iterates the array into a single value using the callback function. 4
Array_reverse () switches the elements in the original array sequentially, creates a new array, and returns the result. 4
Array_search () searches for a given value in the array. If the value is successful, the corresponding key name is returned. 4
Array_shift () deletes the first element in the array and returns the value of the deleted element. 4
Array_slice () extracts a value from the array based on the conditions and returns the result. 4
Array_splice () removes a part of the array and replaces it with other values. 4
Array_sum () calculates the sum of all values in the array. 4
Array_udiff () compares data with the callback function to calculate the difference set of the array. 5
Array_udiff_assoc () carries the index check to calculate the difference set of the array, and compares the data with the callback function. 5
Array_udiff_uassoc () carries the index check to calculate the difference set of the array, and compares data and indexes with the callback function. 5
Array_uintersect () calculates the intersection of arrays and compares the data with the callback function. 5
Array_uintersect_assoc () includes an index check to calculate the intersection of arrays and compares data with a callback function. 5
Array_uintersect_uassoc () includes an index check to calculate the intersection of arrays and compares data and indexes with a callback function. 5
Array_unique () deletes repeated values in the array. 4
Array_unshift () inserts one or more elements at the beginning of the array. 4
Array_values () returns all values in the array. 4
Array_walk () applies user functions to each member in the array. 3
Array_walk_recursive () recursively applies user functions to each member in the array. 5
Arsort () sorts arrays in reverse order and maintains the index relationship. 3
Asort () sorts arrays and maintains the index relationship. 3
Compact () creates an array, including the variable names and their values. 4
Count () calculates the number of elements in the array or the number of attributes in the object. 3
Current () returns the current element in the array. 3
Each () returns the current key/value pair in the array and moves the array pointer one step forward. 3
End () points the internal pointer of the array to the last element. 3
Extract () imports variables from the array to the current symbol table. 3
In_array () check whether the specified value exists in the array. 4
Key () gets the key name from the associated array. 3
Krsort () sorts arrays in reverse order by key name. 3
Ksort () sorts arrays by key names. 3
List () assigns values in the array to some variables. 3
Natcasesort () uses the "natural sorting" algorithm to sort arrays by case-insensitive letters. 4
Natsort () uses the "natural sorting" algorithm to sort arrays. 4
Next () moves the internal pointer in the array one by one. 3
The alias of pos () current. 3
Prev () returns the internal pointer of the array to one position. 3
Range () creates an array containing elements of a specified range. 3
Reset () points the internal pointer of the array to the first element. 3
Rsort () sorts arrays in reverse order. 3
Shuffle () sorts the elements in the array in random order. 3
The alias of sizeof () count. 3
Sort () sorts arrays. 3
Uasort () uses a user-defined comparison function to sort the values in the array and maintain the index Association. 3
Uksort () uses a user-defined comparison function to sort the key names in the array. 3
Usort () uses a user-defined comparison function to sort the values in the array. 3
Next let's take a look at array operations.


<? Php
$ Arr = array ('a' => "You", 'B' => "I", "he ");
$ Arr [] = "other ";
Echo $ arr ['B']. "<br> ";
$ Arr ['c'] = ""; // returns a null value, but still occupies the position
Echo count ($ arr). "<br>"; // The number of values in the array.
Unset ($ arr ['B']); // This function can deregister a string, the entire array equivalent type, and the reference type.
Print_r ($ arr); // This function prints the entire internal structure of the numeric value and reference type.
Echo "<br> ";
Foreach ($ arr as $ key => $ value)
Echo $ key. ":". $ value. "<br>"; // cyclically outputs the value of the entire array.
?>

Array character conversion


<? Php
$ Arr = array ('a' => "You", 'B' => "I", "he ");
Echo $ arr = implode ('-', $ arr); // Convert an array to a string, connector-
Echo "<br> ";
Print_r (explode ('-', $ arr, 2); // convert a string to an array. If the last parameter is not used, all '-' are split into arrays.
?>

Php Data sorting


<? Php
$ Arr = array ('B' => "You", 'a' => "I", "he ");
Ksort ($ arr); // pinyin (UTF-8 encoding) sorting of array key values. The key values are not lost. Note that this sorting does not return a new array, but directly transmits the original array as a reference.
Print_r ($ arr );
Echo "<br> ";
Asort ($ arr); // The array is sorted by the value of pinyin (UTF-8 encoding), and the key value is not lost. If you do not want a key value, you can use the function sort (); if there is also a function rsort () in reverse order (). Note that this sorting does not return a new array, but directly transmits the original array as a reference.
Print_r ($ arr );
Echo "<br> ";
$ Arr = array (10000,100,100 0 );
Natsort ($ arr); // sort values by numbers, while natcasesort () is case insensitive.
Print_r ($ arr );
Echo "<br> ";
Print_r (array_reverse ($ arr); // reverse array order
Echo "<br> ";
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.