Jump to content

[SOLVED] Advanced array removal and looping


Dale_G

Recommended Posts

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?

 

 

Link to comment
Share on other sites

<?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;
}

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.