PHP Next in an Associative Array…

This might me useful for some people…

If you want to echo the value of the next ‘key->value pair’ in an associative array. You can use:

$keys = array_keys($arr);
echo $arr[$keys[(array_search($key, $keys)+1)]];

Where $arr is the associative array, $keys is the $arr keys as values with numerical keys and $key is the $arr key of the key->value pair prior to the one you want to output.


$arr = array(
'12' => 'a',
'14' => 'b',
'20' => 'c',
'23' => 'd'

And you want to output the next value after ’14’=>’b’, which would be ‘c’. All you need to do is define $key = 14;

If you want to output the previous to ’14’=>’b’, just change the “+” to a “-“.

PHP Next in an Associative Array…

PHP Cleaning Arrays…

When arrays are built by user input. You can sometimes experience “blank” values within that array. Sometimes set to “0”, “FALSE” or “NULL”.

You can use array_filter to remove empty (NULL) elements:

$emptyRemoved = array_filter($linksArray);

If you have (int) 0 in your array, you may use the following:

$emptyRemoved = remove_empty($linksArray);

function remove_empty($array) {
 return array_filter($array, '_remove_empty_internal');

function _remove_empty_internal($value) {
 return !empty($value) || $value === 0;

Maybe your elements are not empty per say but contain one or more spaces… You can use the following before using array_filter

$trimmedArray = array_map('trim', $linksArray);


Sourced from:

PHP Cleaning Arrays…