PHP数组按位
问题描述:
如果我有标志的数组,我想他们按位一起结合
if I have an array of flags and I want to combine them with a bitwise conjunction
例如:
$foo = array(flag1, flag2);
到
$bar = flag1 | flag2;
PHP是否有什么好的功能,将做到这很好地为我了吗?
Does PHP have any good functions that will do this nicely for me already?
答
The array_reduce
will reduce an array to a single value for you:
$res = array_reduce($array, function($a, $b) { return $a | $b; }, 0);
减少有时也被称为折(折向左或向右折叠)的其他语言。
Reduce is also sometimes called fold (fold left or fold right) in other languages.