By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

shortening an array

P: n/a
how does one shorten an array based on a key value.

Say I have this:

array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Fo rd","Car4"=>"Toyota");

and I want to pop "Car2" out of the array, leaving the rest of the elements.

array_pop appears to just pop the last element off the array - I want to pop
ANY element out of the array, thus shortening it.

Is there an easy way to do this, without resorting to iteration loops?

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
kaptain kernel wrote:
how does one shorten an array based on a key value.

Say I have this:

array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Fo rd","Car4"=>"Toyota");

and I want to pop "Car2" out of the array, leaving the rest of the elements.
First thing I thought about was

unset($array_name['Car2']);

array_pop appears to just pop the last element off the array - I want to pop
ANY element out of the array, thus shortening it.

Is there an easy way to do this, without resorting to iteration loops?


You might want to check array_slice at
http://www.php.net/array_slice

--
..sig
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
kaptain kernel wrote:
how does one shorten an array based on a key value.

Say I have this:

array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Fo rd","Car4"=>"Toyota");

and I want to pop "Car2" out of the array, leaving the rest of the
elements.


First thing I thought about was

unset($array_name['Car2']);

That works fine Pedro. I didn't realise that you could unset a specific
array element.

Thanks!
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.