Removing Empty Array Keys in PHP

TIL how to remove empty array keys that don’t hold any value, e.g. when passing multiple query parameters in an array via GET, the array_filter function will help to clean your array.

array_filter — Filters elements of an array using a callback functi…


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream

TIL how to remove empty array keys that don't hold any value, e.g. when passing multiple query parameters in an array via GET, the array_filter function will help to clean your array.

array_filter — Filters elements of an array using a callback function […]
Iterates over each value in the array passing them to the callback function. […]
If no callback is supplied, all empty entries of array will be removed.

In its simplest form, suitable for the use case from above, the array_filter function looks like this:

$params = [];
$params = $_GET( $params );
$params = array_filter( $params );

This code will return the array with only the keys that do hold a value. There’s always something new to learn…

Learn more about array_filter in the PHP.net manual


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream


Print Share Comment Cite Upload Translate Updates
APA

foobartel.com :: Stream | Sciencx (2021-09-05T22:00:00+00:00) Removing Empty Array Keys in PHP. Retrieved from https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/

MLA
" » Removing Empty Array Keys in PHP." foobartel.com :: Stream | Sciencx - Sunday September 5, 2021, https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/
HARVARD
foobartel.com :: Stream | Sciencx Sunday September 5, 2021 » Removing Empty Array Keys in PHP., viewed ,<https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/>
VANCOUVER
foobartel.com :: Stream | Sciencx - » Removing Empty Array Keys in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/
CHICAGO
" » Removing Empty Array Keys in PHP." foobartel.com :: Stream | Sciencx - Accessed . https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/
IEEE
" » Removing Empty Array Keys in PHP." foobartel.com :: Stream | Sciencx [Online]. Available: https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/. [Accessed: ]
rf:citation
» Removing Empty Array Keys in PHP | foobartel.com :: Stream | Sciencx | https://www.scien.cx/2021/09/05/removing-empty-array-keys-in-php-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.