Jump to content

[SOLVED] Opposite to array_push() ?


marmite

Recommended Posts

I'm searching php.net for this and having no luck.

 

I want to REMOVE one line from an array, and shift all the associated IDs down by one.

 

E.g.

 

Array:

1 => apple

2 => orange

3 => banana

 

somefunction(remove item 2)

 

Resulting array:

1 => apple

2 =>banana

 

Any ideas ???

 

Thanks

Emma

Link to comment
Share on other sites

Thanks for this, it certainly beats the hours I just spent getting array_values() and unset() to work :)

 

However, I have a new problem. Array_splice reindexes from 0. Can I get it to reindex from 1 instead?

 

The problem is that when 0 is used, if the user wants to delete that item, they are taken to "cart.php?deleteid=0" (before being swiftly relocated)

 

System thinks "deleteid=0" is equivalent to deleteid not being set, so my test fails and the item is not deleted.

 

Any thoughts greatly appreciated.

Link to comment
Share on other sites

That is probably because you're using something like:

<?php
if (!empty($_GET['deleteid'])) {
//
// do the code
//
}
?>

When you really want to do

<?php
if (isset($_GET['deleteid'])) {
//
// do the code
//
}
?>

 

When a value is 0, the function empty() will return TRUE.

 

Ken

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.