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