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
https://forums.phpfreaks.com/topic/89747-solved-remove-values-from-an-array/
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);
?>

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.