Categories
PHP

aray_map() and array_walk()

How to modify all array elements, or nested array elements using array_map() and array_walk() functions. What is the difference between array_walk() and array_map() functions.

Categories
PHP

array_filter(): Filtering Arrays

The array_filter() function filters the elements of an array by applying a callback function to each element to decide whether it appears in the output array. The callback function returns true upon success and false otherwise.

Categories
PHP

Finding values and keys in arrays

Check if a value or key exists in the array with in_array and array_key_exists functions respectively. Search the array by value and get first or all corresponding keys from the array with array_search and array_keys functions.

Categories
PHP

Shuffle Array and Get Random Elements

If you want to randomize the order of elements in the array use the shuffle() function. Or use array_rand() function to grab one or more random elements out of an array.

Categories
PHP

Natural Order Sorting

Natural ordering is an alphanumeric sort order, it treated multiple digits as a single character. The natsort() and natcasesort functions do not take flags and are the same as called sort() (or sort(), rsort() etc.) with the SORT_NATURAL flag.

Categories
PHP

Sorting Nested Arrays

PHP sorting functions can not perform a deep sort on nested arrays. To solve this problem, create a recursive function that calls any PHP sorting function and sorts a nested array deeply (by calling a sorting function recursively).

Categories
PHP

Sorting Multidimensional and Multiple Arrays with array_multisort

In this tutorial, you’ll learn how to use the array_multisort() function to sort multiple arrays depending on the sort order of another array. You’ll also learn to use this function to sort multidimensional arrays in one or more dimensions.

Categories
PHP

Sorting with user-defined comparison function

The sorting functions, described in previous tutorials, sort elements in alphabetic, numeric, or alphanumeric order. To sort elements based on user-defined criteria, PHP provides three functions: usort, uasort, and uksort.

Categories
PHP

Sorting Associative Arrays

It’s often desirable to keep the key/value associations when sorting associative arrays. To maintain the key/value association the asort( ) and arsort( ) functions are used. Rather than sort on element values, the ksort( ) and krsort( ) functions rearrange elements in an array by sorting on the keys or the indexes.

Categories
PHP

Sorting Arrays

The simplest array-sorting functions are sort( ) and rsort( ), which rearrange the elements of the subject array in ascending and descending order, respectively: