PHP Development notes series (9)-array (3 )??? I wrote two articles about the daily use of Php arrays. this article "PHP Development notes series (9)-array (3)" focuses on the size and sorting of arrays .??? 1. get the array length ??? The count () function returns the number of elements in the array, which is the alias of sizeof () and count (). The function is the same .? PHP Development notes series (9)-array (3)
??? I wrote two articles about the daily use of Php arrays. this article "PHP Development notes series (9)-array (3)" focuses on the size and sorting of arrays.
??? 1. get the array length
??? The count () function returns the number of elements in the array, which is the alias of sizeof () and count (). The function is the same.
?
file: count.phpurl: http://localhost:88/array/count.php
?
??? In addition to accepting the first array variable to be counted, the count () function can also accept a mode parameter to specify whether recursion is required to count the number of all elements in the multi-dimensional array, the code is as follows:
?
file: count.phpurl: http://localhost:88/array/count.php
?
??? 2. count the occurrence frequency of array elements
??? The array_count_values () function returns an array containing the associated key/value. For example, 'A' appears twice, 'B' appears once, and so on. The code is as follows:
?
file: array_count_values.phpurl: http://localhost:88/array/array_count_values.php
====================
"; $favor1 = array('1'=>'Sport', '2'=>'Sing', '3'=>'Sing', '4'=>'Travel'); $stats1 = array_count_values($favor1); print_r($stats1); ?>
?
??? 3. return non-repeating elements in the array
??? The array_unique () function deletes all repeated values in the array and returns an array composed of non-repeated values. the original array elements remain unchanged. the code is as follows:
?
file: array_unique.phpurl: http://localhost:88/array/array_unique.php
====================
"; print_r($unique); ?>
?
??? 4. array sorting
??? The sort () function sorts arrays, and each element is sorted in a descending order of values. The sort () function does not regret the array after sorting. On the contrary, it only sorts, regardless of the result, it does not regret any value. In addition, you can specify sorting rules for sorting. for example, SORT_NUMERIC is ordered by numerical values and applied to integers and floating-point numbers. SORT_REGULAR is ordered by ASCII values and applied to characters or strings, SORT_STRING is sorted by string. The code is as follows:
??
??? Note: the sort () function directly modifies the value of the passed-in array and carefully checks the output result of the following program.
?
File: sort. phpurl: http: // localhost: 88/array/sort. php
'; Sort ($ number, SORT_NUMERIC); print_r ($ number); echo"
================================
"; $ Letter = array ('A', 'A', 'z', 'F', 'G', 'E'); print_r ($ letter); echo'
'; // The default mode is SORT_REGULAR sort ($ letter, SORT_REGULAR); print_r ($ letter); echo"
================================
"; $ String = array ('Jack', 'Mike ', 'Mary', 'jassica ', 'Ruby'); print_r ($ string); echo'
'; Sort ($ string, SORT_STRING); print_r ($ string) ;?>
?
??? From the above results, we can see that the sort () function is modified not only by the elements in the array, but also by changing the correspondence between keys and values, to maintain the relationship between key-value pairs, you need to use the asort () function, as shown below:
?
File: asort. phpurl: http: // localhost: 88/array/asort. php
'; Asort ($ number, SORT_NUMERIC); print_r ($ number); echo"
================================
"; $ Letter = array ('A', 'A', 'z', 'F', 'G', 'E'); print_r ($ letter); echo'
'; // The default mode is SORT_REGULAR asort ($ letter, SORT_REGULAR); print_r ($ letter); echo"
================================
"; $ String = array ('Jack', 'Mike ', 'Mary', 'jassica ', 'Ruby'); print_r ($ string); echo'
'; Asort ($ string, SORT_STRING); print_r ($ string) ;?>
?
??? 5. sort in reverse order
??? The rsort () function is similar to the sort function. There are also modes for maintaining the key-value relationship and not holding the key-value relationship. the method is the same.
??? 6. natural sorting of arrays
??? In the sequence of several groups, such as student1.jpg, student2.jpg, student10.jpg, example, studdent10.jpg, student2.jpg, example, student2.jpg, studdent10.jpg, and studentw.jpg. Now you need to use the natsort () function.
?
file: natsort.phpurl: http://localhost:88/array/natsort.php
==========================
"; sort($student); print_r($student); echo "
==========================
"; natsort($student); print_r($student); ?>
?
??? The natsort () function also has a deformation called the natcasesort () function, which is functionally the same as the natsort () function, but is case-insensitive.
?
file: natcasesort.phpurl: http://localhost:88/array/natcasesort.php
?
??? 7. sort arrays by key values
??? The preceding function sorts the value key array. if you want to sort the joined key array, you may have special requirements, such as sorting by key value. in this case, you need to use the ksort () function, TRUE is returned for success, and FALSE is returned for failure. Also, like the sort () function, it accepts a sort_flags parameter, which is SORT_NUMERIC, SORT_REGULAR, and SORT_STRING, specifying the sorting mode. The code is as follows:
?
file: ksort.phpurl: http://localhost:88/array/ksort.php
==========================
"; ksort($student); print_r($student); ?>
?
??? 8. Custom sorting rules
??? Php also provides a custom sorting method, which is usort (). you can use the user-defined comparison algorithm specified in this function to sort arrays. However, it is usually used less. you can experiment with it yourself.
???
??? The sorting part of the Php array is described here. next time, we will continue the subsequent content of the Php array.
??? Address: http://ryan-d.iteye.com/blog/1566686
?
?