PHP数组按​​位 [英] php array bitwise

查看:134
本文介绍了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?

推荐答案

array_reduce 将降低一个数组单一值给你:

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.

这篇关于PHP数组按​​位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆