How to delete a key and return the value from a PHP array? -


when using php, find myself writing code lot:

$target = $_session[after_login_target]; unset($_session[after_login_target]); return $target; 

in python, there dict.pop method let me similar in 1 statement, without temporary variable:

return session.pop(after_login_target) 

is there similar function or trick in php?

i don't see built-in function this, can create own.

/**  * removes item array , returns value.  *  * @param array $arr input array  * @param $key key pointing desired value  * @return value mapped $key or null if none  */ function array_remove(array &$arr, $key) {     if (array_key_exists($key, $arr)) {         $val = $arr[$key];         unset($arr[$key]);          return $val;     }      return null; } 

you can use array, e.g. $_session:

return array_remove($_session, 'after_login_target'); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -