0

`Say I have an associative array like so:

$array = array(
 'names' => array('amy','john', 'peter'),
 'places' => array('milan', 'venice', 'amsterdam'),
 'uncategorized' => array('desk', 'plane', 'silly'),
 'jobs' => array('singer', 'clerk', 'broker') 
);

Now suppose I want to save uncategorized into a new array and remove it from $array, I can do:

$uncategorized = $array['uncategorized'];
unset($array['uncategorized']);

My question is, is there any single function that does it kind of like how array_pop returns the last value and shortens the array by that element?

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69
  • Not one function, but this could work if you're sure that the key will always be there: `$uncategorized = array_splice($array, array_search('uncategorized', array_keys($array), true), 1);`. Not very useful, though, IMHO. – Ja͢ck Feb 27 '15 at 05:39
  • What is wrong which assignment and then unset? This is undoubtedly what would happen under the covers for any function that would work in an equivalent manner. Of course if you really wanted a function call to do this, you could easily define your own function. – Mike Brant Feb 27 '15 at 05:47
  • @MikeBrant - never said anything was wrong with it - I was merely checking if there was any inbuilt function to do that just like how there is array_pop to do same thinf for last element of array. – Undefined Variable Feb 27 '15 at 05:49
  • http://stackoverflow.com/questions/1783089/array-splice-for-associative-arrays – chiliNUT Feb 27 '15 at 05:57

0 Answers0