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 Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/ 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> Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/#findComment-238241 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. Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/#findComment-238300 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. Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/#findComment-238308 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 Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/#findComment-238314 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 Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/#findComment-238321 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 Link to comment https://forums.phpfreaks.com/topic/48643-solved-opposite-to-array_push/#findComment-238326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.