Top PHP Functions for Array and String Manipulation

Photo by NITISH GOSWAMI on Unsplash

Top PHP Functions for Array and String Manipulation

PHP Feb 4, 2023

Arrays and strings are two important data structures in PHP that allow you to store and manipulate data in various ways. Here, we will look at some of the most commonly used functions for manipulating arrays and strings in PHP.

Array Functions in PHP:

sizeof($array): This function is used to find out the number of elements in an array.

Example:

$array = array(1, 2, 3, 4, 5);
$num_elements = sizeof($array);
echo $num_elements;

#Output: 5


array_values($array): This function is used to retrieve all the values from an associative array.

Example:

$array = array("a" => 1, "b" => 2, "c" => 3);
$values = array_values($array);
print_r($values);

#Output: Array ( [0] => 1 [1] => 2 [2] => 3 )


array_keys($array): This function is used to retrieve all the keys from an associative array.

Example:

$array = array("a" => 1, "b" => 2, "c" => 3);
$keys = array_keys($array);
print_r($keys);

#Output: Array ( [0] => a [1] => b [2] => c )


array_pop($array): This function is used to remove an element from the end of an array.

Example:

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

#Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )


array_push($array, $value): This function is used to add an element to the end of an array.

Example:

$array = array(1, 2, 3, 4, 5);
array_push($array, 6);
print_r($array);

#Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )


array_shift($array): This function is used to remove an element from the beginning of an array.

Example:

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

#Output: Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )


array_unshift($array, $value): This function is used to add an element to the beginning of an array.

Example:

$array = array(1, 2, 3, 4, 5);
array_unshift($array, 0);
print_r($array);

#Output: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )


array_map($callback, $array1): This function is used to apply a callback function to each element of an array and return a new array with the results.

Example:

$array = array(1, 2, 3, 4, 5);
$callback = function($value) {
return $value * 2;
};
$new_array = array_map($callback, $array);
print_r($new_array);

#Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 )


sort($array): The sort() function sorts an array in ascending order.

Example:

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

#Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )


array_reverse($array): The array_reverse() function reverses the order of elements in an array.

Example:

$array = array(1, 2, 3, 4, 5);
$new_array = array_reverse($array);
print_r($new_array);

#Output: Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )


array_merge($array1, $array2): The array_merge() function merges one or more arrays into a single array.

Example:

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);
$new_array = array_merge($array1, $array2);
print_r($new_array);

#Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )


array_rand($array): The array_rand() function returns a random key from an array.

Example:

$array = array("John", "Jane", "Jim");
$random_key = array_rand($array);
echo "The random key is $random_key and the value is $array[$random_key].";

#Output: The random key is 2 and the value is Jim.


array_search($search, $array): The array_search() function searches for a value in an array and returns the key if found.

Example:

$array = array("John", "Jane", "Jim");
$search = "Jane";
$key = array_search($search, $array);
echo "The value $search is found at key $key.";

#Output: The value Jane is found at key 1.


array_slice($array, $start, $length): The array_slice() function extracts a portion of an array and returns a new array.

Example:

$array = array(1, 2, 3, 4, 5);
$new_array = array_slice($array, 2, 2);
print_r($new_array);

#Output: Array ( [0] => 3 [1] => 4 )


array_unique($array): The array_unique() function removes duplicate values from an array.

Example:

$array = array(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
$new_array = array_unique($array);
print_r($new_array);

#Output:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )


extract($array): The extract() function creates variables from an array.

Example:

$array = array("name" => "John", "age" => 30);
extract($array);
echo "My name is $name and I am $age years old.";

#Output: My name is John and I am 30 years old.


array_key_exists($key, $array): The array_key_exists() function checks if a key exists in an array.

Example:

$array = array("name" => "John", "age" => 30);
$key = "name";
if (array_key_exists($key, $array)) {
echo "The key $key exists in the array.";
} else {
echo "The key $key does not exist in the array.";
}

#Output: The key name exists in the array.


in_array($value, $array): The in_array() function checks if a value exists in an array.

Example:

$array = array("John", "Jane", "Jim");
$value = "Jim";
if (in_array($value, $array)) {
echo "The value $value exists in the array.";
} else {
echo "The value $value does not exist in the array.";
}

#Output: The value Jim exists in the array.

String Functions in PHP:

substr($string, $, $): The substr() function is used to extract a portion of a string. It takes three arguments: the original string, the starting position, and the length of the desired substring.

Example:

$string = "Hello World";
$substring = substr($string, 0, 5);
echo $substring;

#Output: Hello


strlen($string): The strlen() function is used to find the length of a string. It takes one argument: the original string.

Example:

$string = "Hello World";
$length = strlen($string);
echo $length;

#Output: 11


trim($string): The trim() function is used to remove whitespaces from the beginning and end of a string. It takes one argument: the original string.

Example:

$string = "    Hello World     ";
$trimmed = ltrim($string);
echo $trimmed;

#Output: Hello World


ltrim($string): The ltrim() function is used to remove whitespaces from the beginning of a string. It takes one argument: the original string.

Example:

$string = "    Hello World";
$trimmed = ltrim($string);
echo $trimmed;

#Output: Hello World


rtrim($string): The rtrim() function is used to remove whitespaces from the end of a string. It takes one argument: the original string.

Example:

$string = "Hello World    ";
$trimmed = rtrim($string);
echo $trimmed;

#Output: Hello World


strtolower($string): The strtolower() function is used to convert a string to lowercase. It takes one argument: the original string.

Example:

$string = "HELLO WORLD";
$lower = strtolower($string);
echo $lower;

#Output: hello world


strtoupper($string): The strtoupper() function is used to convert a string to uppercase. It takes one argument: the original string.

Example:

$string = "hello world";
$upper = strtoupper($string);
echo $upper;

#Output: HELLO WORLD


str_replace($search, $replace, $string): The str_replace() function is used to replace a specific portion of a string with another string. It takes three arguments: the search string, the replacement string, and the original string.

Example:

$string = "Hello world!";
$search = "world";
$replace = "universe";
$result = str_replace($search, $replace, $string);

#Output: Hello universe!


explode(delimiter, string): explode() function The explode() function is used to split a string into an array based on a specified delimiter.

Example:

$string = "Hello,world,!";
$delimiter = ",";
$result = explode($delimiter, $string);

#Output: Array ( [0] => Hello [1] => world [2] => ! )


implode(glue, array): implode() function The implode() function is used to join the elements of an array into a single string.

Example:

$array = Array ( "Hello", "world", "!" );
$glue = " ";
$result = implode($glue, $array);

#Output: "Hello world !"


These are some of the most commonly used array and string functions in PHP. They are extremely useful in various applications and can help to manipulate and process data in an efficient manner.

Tags

Anurag Deep

Logical by Mind, Creative by Heart