Rhys
Rhys

Reputation: 1581

Alternative to eval() for an array of bitwise operation on an array

I have an array that I want to use with the the bitwise inclusive or operation.

The two solutions at the moment are:

A foreach() loop that evaluates them in order

$final = 0;
foreach($bits as $bit)
  $final = $final | $bit;
}

Imploding the array with the bitwise OR as the glue, and eval() the string

eval(implode(' | ', $bits));

Are these the only options or am I missing a simple native array method?

Upvotes: 2

Views: 86

Answers (1)

Rhys
Rhys

Reputation: 1581

Following @mario's comment, the solution is:

array_sum(array_unique($bits));

Upvotes: 2

Related Questions