Remove every third entry from array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
computer guy
Forum Newbie
Posts: 2
Joined: Thu Jun 17, 2010 8:52 pm

Remove every third entry from array?

Post by computer guy »

Hello everyone,

I need some help with removing entries from an array. I have the following array

Code: Select all

            [0] => Yr7
            [1] => TEC4
            [2] => 6
            [3] => Yr8
            [4] => TEC4
            [5] => 6
            [6] => Yr9
            [7] => DATZ
            [8] => 6
            [9] => Yr10
            [10] => DATZ
            [11] => 6
            [12] => Yr11
            [13] => CON
            [14] => 9
            [15] => Yr11
            [16] => DAT
            [17] => 2
            [18] => Yr12
            [19] => CON
            [20] => 9
            [21] => Yr12
            [22] => ISO
            [23] => 1
            [24] => Yr12
            [25] => PGD
            [26] => 3

What I want to do is remove all of the array values that are a single number, i.e. [2], [5], [8], etc. and then join the others in pairs, i.e. [0] => Yr7TEC4, [1] => Yr8TEC4
Does anyone know how I might go about doing this?

Thank you :)
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Remove every third entry from array?

Post by Phoenixheart »

Not really optimized, but you may try

Code: Select all

$i = 0;
while ($source[$i])
{
    if (!($i + 1) % 3) continue; // the third entry
    if (!$i % 2) $target[] = $source[$i]; // the first element creates a new item
    else $target[count($target) - 1] .= $source[$i]; // the second element appends the last item
    ++$i;
}

print_r($target);
It's not tested and may have bugs, but it should give you the point.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Remove every third entry from array?

Post by cpetercarter »

Code: Select all

$i = 0;
$j = 0;
$newarray = array();
foreach ($oldarray as $item) {
      if ($i == 0) {
           $newarray[$j] = $item;           
            }
      if ($i  == 1) {
            $newarray[$j] .= $item;
            $j++;
            }
      if ($i == 2) {
            $i = 0;
             }
      else {
            $i++;
            }
}
computer guy
Forum Newbie
Posts: 2
Joined: Thu Jun 17, 2010 8:52 pm

Re: Remove every third entry from array?

Post by computer guy »

Thank you both. I ended up going with cpetercarter's idea and it worked great. :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Remove every third entry from array?

Post by AbraCadaver »

Assuming that the array is always structured like this and there are no additional checks, this is shorter:

Code: Select all

for($i=0; $i<count($array); $i+=3) {
	$new[] = implode(array_slice($array, $i, 2));
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply