Jump to content

[SOLVED] Remove values from an array


Ninjakreborn

Recommended Posts

Let's assume I have an array as follows:

array(

[0] = 13

[1] = 15

[2] = 17

[3] = 35

[4] = 65

[5] = 77

[6] = 83

[7] = 95

)

 

Assume that is my current array above.  Now let's say I wanted to take and remove 35 from that array.  So we would be left with all of those key/value pairs except the one's I wanted to remove.  I need to figure out how to do roughly the same thing except it would be dynamic.

I am creating an array of years (which I have).  Then I have a list of excluded years.  I ned to be able to remove all variables that are present from the excluded years list out of the valid years.  For example I might have

 

Years to keep

array(

[0] = 2004

[1] = 2005

[2] = 2006

[3] = 2007

[4] = 2008

[5] = 2009

[6] = 2010

)

 

Years to remove

array (

[0] = 2006

[1] = 2009

)

Now how would I make it so that all of the years to remove, are taken out of the years to keep array so I just have the years left that I need?

Link to comment
Share on other sites

Ah ok, but I found a good solution too.

<?php
// years to keep creation
$years_list = $years;
// add one year
$years_list[] = min($years) - 1;
// remove one year
$years_list[] = max($years) + 1;
// order them
sort($years_list);
// here is what I used to take and remove everything that is in years list that is in years
$years_list = array_diff($years_list, $years);
// reorder them again
sort($years_list);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.