views:

48

answers:

1

Something about using $array_increment++ to fill an array seem inefficient, even though it works.

Is there a more efficient way to fill $color_names in the code below than using a variable to walk through the array? Since I'm using a foreach and 'if' to fill the array, it's harder to figure out a different way than using ++.

$array_increment = 0;    
foreach ($tokens as $token)
    {
        if(is_array($token))
        {
            if(token_name($token[0]) === "T_STRING")
            {
            $color_names[$array_increment] = $token[1];
            $array_increment++;
            }
        }

    }
+5  A: 

Instead of

$color_names[$array_increment] = $token[1];
$array_increment++;

You can just do:

$color_names[] = $token[1];

PHP automatically uses incrementing array keys.

codaddict
This works thanks
JMC