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
https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/
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.

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

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.