HardCoreMore Posted September 7, 2010 Share Posted September 7, 2010 Hi all, Is there a function or method to delete element from current position in array without knowing the index. For example: $ar = array( 1, 2, 3, "test", 4, 5, "test2" ); while( $v = current( $ar ) ) { if( key( $ar ) == "test" ) { // remove element from the array where key equals "test" // i could count every cycle and splice from that index but i wonder can that be done without it // i am looking at the php array functions but i can't find any that will delete from element from current position in array echo 'key( $ar ) je test. key( $ar ): ' . key( $ar ); } next( $ar ); } One thing that i don't understand is that when i run example above the "test" key is found and when i echo it it gives me 0. Link to comment https://forums.phpfreaks.com/topic/212761-remove-element-from-current-position-in-array/ Share on other sites More sharing options...
Pikachu2000 Posted September 7, 2010 Share Posted September 7, 2010 Do you mean like this? From your description, I think something like this will work for what you are trying to do. <?php $ar = array( 1, 2, 3, "test", 4, 5, "test2" ); $value = 'test'; foreach( $ar as $k => $v ) { if( $v == $value ) { unset( $ar[$k] ); } } echo '<pre>'; print_r($ar); echo '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/212761-remove-element-from-current-position-in-array/#findComment-1108253 Share on other sites More sharing options...
HardCoreMore Posted September 7, 2010 Author Share Posted September 7, 2010 yes that is quite good solution. i just wondered if above way is possible. thanks for reply Link to comment https://forums.phpfreaks.com/topic/212761-remove-element-from-current-position-in-array/#findComment-1108257 Share on other sites More sharing options...
HardCoreMore Posted September 7, 2010 Author Share Posted September 7, 2010 but when we are here at arrays do you know is moving through array faster with current and next functions or by using for each Link to comment https://forums.phpfreaks.com/topic/212761-remove-element-from-current-position-in-array/#findComment-1108259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.