marmite Posted April 25, 2007 Share Posted April 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted April 25, 2007 Share Posted April 25, 2007 <pre> <?php $data = array ( 'apple', 'orange', 'banana' ); print_r($data); array_splice($data, 1, 1); print_r($data); ?> </pre> Quote Link to comment Share on other sites More sharing options...
marmite Posted April 25, 2007 Author Share Posted April 25, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 25, 2007 Share Posted April 25, 2007 <pre> <?php $data[1] = 'apple'; $data[2] = 'orange'; $data[3] = 'banana'; print_r($data); array_splice($data, 1, 1); array_unshift($data, ''); unset($data[0]); print_r($data); ?> </pre> A more advanced function is in the User Notes. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
marmite Posted April 25, 2007 Author Share Posted April 25, 2007 I was just using if($_GET['deleteid']) - is that bad practise? Works now - only three hours on this silly thing! Thanks for your help Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 The statement "f($_GET['deleteid']))" will return FALSE when the value is 0 also. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.