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. Quote Link to comment 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>'; ?> Quote Link to comment 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 Quote Link to comment 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 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.