PHP Basics: Arrays, Functions, and Strings

Sorting PHP arrays

sort() sorts the elements contained in the array in ascending order; the sort() function is case-sensitive.

All uppercase letters come after lowercase letters.

The asort() function and the ksort() function sort associative arrays

For example: $a=array(‘name’=>100,’user’=>200,’title’=>300);

The asort() function sorts by each element value in the array $a. In the array $a the element values are numbers.

The ksort() function arranges the array’s keys in alphabetical order.

Reverse sorting an array

The rsort() function sorts a one-dimensional numeric indexed array in descending order

The arsort() function sorts a one-dimensional associative array in descending order of each element value.

The krsort() function arranges a one-dimensional array in descending order according to the array elements’ keys.

Reordering an array The shuffle() function randomly reorders the array’s elements.

The array_reverse() function gives a reversed ordering of the original array.

Walking through an array in order The prev() function arranges a one-dimensional array in descending order.

The each() function returns the current element before moving the pointer forward one position (that is, it returns the current element and at the same time outputs an array).

The next() function moves the pointer forward, and then returns the new current element (that is, it returns the current element).

The array_walk() function uses or modifies each element in the array in the same way.

Counting elements The array_count_values() function counts how many times each particular value has appeared in the array. It returns an associative array containing the frequencies. The count() and sizeof() functions are used in the same way.

The extract() function can take a non-numeric indexed array that also has many key-value pairs and convert it into a series of scalar variables.

Output functions echo and print both output strings to the browser, but print() has a return value of true or false. The printf() function outputs a formatted string to the browser

The sprintf() function returns a formatted string.

Splitting functions The explode() function splits the string itself into small pieces according to a specified delimiter string, and returns the split pieces in an array.

The implode() function (alias join) returns a string composed of the array’s elements.

The strtok() function takes out only a few fragments from a string at a time.

The substr() function gets the string between a given start point and end point in a string.

Changing the case of letters in a string The strtoupper() function converts a string to uppercase.

The strtolower() function converts a string to lowercase.

The ucfirst() function, if the string’s first character is a letter, converts that string’s first letter to uppercase.

The ucwords() function converts the first letter of each word in a string to uppercase.

Comparing strings The strcmp(str1, str2) function returns 0 if the two strings are equal, returns a positive number if str1 comes after str2 in dictionary order (greater than str2), and returns a negative number if str1 is less than str2.

Apart from not being case-sensitive, the strcasecmp() function is the same as strcmp().

Apart from not being case-sensitive, the strnatcmp() function is the same as strcmp(); however the strnatcmp() function compares strings using natural ordering.

The strlen() function checks the length of a string.

Finding a string within a string The strstr() function searches for a matching string or character within a longer string. The strchr() function is the same as the strstr() function. The stristr() function is the same as the strstr() function, except that it is not case-sensitive.

The strrchr() function is the same as the strstr() function, but it returns the searched string from before the position where the target keyword last appears. Finding the position of a string

The strpos() function returns the position of the target keyword substring within the searched string. The strpos() function runs faster than the strstr() function. The strrpos() function returns the position of the last occurrence of the target keyword substring in the searched string.

If it does not exist, it returns false. Replacing strings mixed str_replace(mixed needle, mixed new_needle, mixed haystack[, int & count]); replaces part of a string with another string. neddle is the string to be processed, new_needle is the string to be inserted, haystack is the starting position of the string, with the start position being 0, and count is the number of replacement operations performed.

The substr_replace() function is used to find and replace a specific substring within a string at a given position.