Dale_G Posted November 21, 2008 Share Posted November 21, 2008 Say I have this array $aych = array( 'B', 'B', 'A', 'A', 'B', 'A', 'A', 'B', 'B', 'A' ); and when i do a print_r right now i get... Array ( [0] => B [1] => B [2] => A [3] => A [4] => B [5] => A [6] => A [7] => B [8] => B [9] => A ) now what i need to happen is, loop through ALL keys in the array and check if there values are B, if they are, then remove it from the array and shift everything forward. foreach ( $aych as $key => $value ) { if ( preg_match( '{B}', $value ) ) { array_splice( $aych, $value, 0 ); } } i was able to loop through them all, and check if the value is B, as shown above. I know you use the array_splice function for this, but im not exactly sure how to properly utilize this in this environment. in the end, if i print_r it, i need to get Array ( [0] => A [1] => A [2] => A [3] => A [4] => A ) which is after all of the B's are removed and the array moved foward. does anyone know how to do this? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 21, 2008 Share Posted November 21, 2008 look at using http://us.php.net/manual/en/function.array-filter.php with a callback function to filter out "B" then to get then indexing back (which I don't know why u need to) try sorting it by key Quote Link to comment Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 foreach($aych as $key => $value) { if ($value === 'B'){ array_splice($aych, $key, 1); } } Not tested, but should work Quote Link to comment Share on other sites More sharing options...
Dale_G Posted November 21, 2008 Author Share Posted November 21, 2008 Not having much luck with array_filter, and gevans after trying your code after a print_r I get Array ( [0] => A ) Quote Link to comment Share on other sites More sharing options...
corbin Posted November 21, 2008 Share Posted November 21, 2008 <?php $aych = array( 'B', 'B', 'A', 'A', 'B', 'A', 'A', 'B', 'B', 'A' ); function RemoveB($c) { return ($c == 'B'); } $a = array_filter($aych, "RemoveB"); sort($a); print_r($a); The sort was just to redo the keys. It would probably be faster to redo them manually though. It would be even faster to do this (most likely): $new_array = array(); foreach($aych as $c) { if($c != 'B') $aych[] = $c; } Quote Link to comment Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 $aych = array( 'B', 'B', 'A', 'A', 'B', 'A', 'A', 'B', 'B', 'A' ); print_r($aych); foreach($aych as $key => $value){ if($value !== 'B'){ $aych2[] = $value; } } echo '<br /><br />'; print_r($aych2); try that instead, tested and working Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 21, 2008 Share Posted November 21, 2008 <?php $array = array( 'B', 'B', 'A', 'A', 'B', 'A', 'A', 'B', 'B', 'A' ); $keys = array_keys($array, 'B'); foreach($keys as $key) { unset($array[$key]); } $array = array_values($array); print_r($array); ?> Another solution. I tried for efficiency (let C do most of the looping, don't append to arrays as its speed can be unpredictable, and save the renumbering for the end)... it benchmarked about twice as fast as corbin's/gevans's second suggestion, but it's just a matter of fractions of microseconds... (pretty much nothing) I'd go for readability, whichever one you find most readable I'd use. Quote Link to comment Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 I hadn't thought of that, I'd go with genericnumber1's solution. I always like to go with the fastest option! Quote Link to comment Share on other sites More sharing options...
Dale_G Posted November 21, 2008 Author Share Posted November 21, 2008 Alright, thank all three of you for a job well done! 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.