Jump to content

remove item from array by key


dmikester1

Recommended Posts

I can't figure out how to remove a specific item from my array by key.

Array:

$uncachedSites = array(2=>'site 1', 3=>'site 2', 4=>'site 3', 5=>'site 4');

 

So I'm running through a loop with $i starting at 2 and going up to 5. If I say unset($uncachedSites[$i], the first time around I'm thinking it will remove 'site 3' instead of 'site 1' like I want it to. But I can't figure out how to remove the first one without just saying unset($uncachedSites[0])

Thanks

Mike

Link to comment
https://forums.phpfreaks.com/topic/274498-remove-item-from-array-by-key/
Share on other sites

did you try it?

 

$uncachedSites = array(2=>'site 1', 3=>'site 2', 4=>'site 3', 5=>'site 4');

for ($i=2; $i<=5; $i++)
{
   if ($uncachedSites[$i]=='site 1') unset($uncachedSites[$i]);
}

echo '<pre>',print_r($uncachedSites, true),'</pre>';

/* results ****
Array
(
   [3] => site 2
   [4] => site 3
   [5] => site 4
)


*/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.