

As before with unset() it won’t change the keys of the array. If you know the values of the array elements which you want to delete, then you can use \array_diff(). If you want to delete multiple array elements and don’t want to call unset() or \array_splice() multiple times you can use the functions \array_diff() or \array_diff_key() depending on whether you know the values or the keys of the elements which you want to delete. You don’t assign the return values of those functions back to the array. \array_splice() needs the offset, not the key, as the second parameter.Īrray_splice(), same as unset(), take the array by reference. If you use \array_splice() the keys will automatically be reindexed, but the associative keys won’t change - as opposed to \array_values(), which will convert all keys to numerical keys. If you want to reindex the keys you can use \array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0.Ĭode: $array = Note that when you use unset() the array keys won’t change. This only works if the element does not occur more than once, since \array_search returns the first hit only. If you know the value and don’t know the key to delete the element you can use \array_search() to get the key. If you want to delete just one array element you can use unset() or alternatively \array_splice(). Use the PHP trim() function to remove whitespaces or other characters from both ends of a string.There are different ways to delete an array element, where some are more useful for some specific tasks than others.
#EMPTY FIRST VALUE PHP EXPLODE CODE#
Output: api/v1/posts Code language: plaintext ( plaintext ) Summary

To remove the slashes ( /) from both ends of the URI, you can use the trim() function: Code language: plaintext ( plaintext ) To get the request URI, you access the $_SERVER array with the key 'REQUEST_URI':
Let’s take some examples of using the PHP trim() function. It returns a new string with all the $characters removed. It’s important to note that the trim() function doesn’t change the input string. For example, to remove all ASCII control characters from both ends of a string, you use the following value for the $characters argument: '\x00.\x1F' Code language: PHP ( php ) To specify a range of characters to remove, you can use the ‘.’. If you want to remove other characters, you can specify them in the $characters argument. It specifies which character to remove from both ends of the $string.īy default, the trim() function removes the following characters: Character $string is the input string that will be trimmed.Here’s the syntax of the trim() function: trim ( string $string, string $characters = " \n\r\t\v\0" ) : string Code language: PHP ( php ) The trim() function removes the whitespaces or other characters from the beginning and end of a string.
#EMPTY FIRST VALUE PHP EXPLODE HOW TO#
Summary: in this tutorial, you’ll learn how to use the PHP trim() function to remove whitespace or other characters from both ends of a string.
