Jump to content

Unsetting an array


thomashw

Recommended Posts

Is it possible to unset an array so the key that is being unset is deleted and all others move back one?

 

For instance:

 

If you had an array which contained planes[1], planes[2], planes[3], etc.

 

And you deleted planes[2], then planes[3] would become planes[2].

 

Making the array to be just planes[1], planes[2], etc.

 

I'm thinking it's possible because I know it's possible in C++.

 

Thanks! :)

Link to comment
https://forums.phpfreaks.com/topic/97298-unsetting-an-array/
Share on other sites

This might work:

 

function array_remove($array, $removed_key) {
     $new_array = array();
     foreach ($array as $key => $value) {
          if ($key != $removed_key) {
               array_push($new_array, $value);
          }
     }
     return $new_array;
}

$_SESSION['name'] = array_remove($_SESSION['name'], $number);

Link to comment
https://forums.phpfreaks.com/topic/97298-unsetting-an-array/#findComment-497926
Share on other sites

I found the function array_values(); does the job - for anyone who sees this in the future! The function goes through the array and indexes everything numerically.

 

So after using the unset(); function, the array_values(); function can be used to completely remove the key from the array and put everything back in order.

Link to comment
https://forums.phpfreaks.com/topic/97298-unsetting-an-array/#findComment-498022
Share on other sites

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.