使用 php 读取类似 CHMOD 的值
# Permission
7 read and write and execute (1+2+4)
6 read and write (2+4)
5 read and execute (1+4)
4 read only
3 write and execute (1+2)
2 write only
1 execute only
0 none
我喜欢这种模式,您可以将选项选项的任意组合存储在一个整数中,并通过将最后一个数字加倍(8、16、32 等)来添加选项。 我想使用这种方法,我想知道它是否有一个名称,以及将数字转换成与此类似的结果的最快最简单的方法是什么?
array(1=>false,2=>true,4=>true);//6
array(1=>true,2=>true,4=>true,8=>true);//15
# Permission
7 read and write and execute (1+2+4)
6 read and write (2+4)
5 read and execute (1+4)
4 read only
3 write and execute (1+2)
2 write only
1 execute only
0 none
I like the pattern that you can store any combination of the options options in one integer number, and add options by doubling the last number (8, 16 ,32 etc).
I'd like to use this method, and I'd like to know if there's a name for it, and what is the fastest simplest method for turning numbers into results similar to this?
array(1=>false,2=>true,4=>true);//6
array(1=>true,2=>true,4=>true,8=>true);//15
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按照建议使用按位运算。为了得到你想要的数组。
该函数将计算出任何给定值所需的位数,并以您建议的格式返回数组。
我已经包含了一个 0 到 20 的循环来验证。
Using bitwise operations as recommended. To get the array you are after.
This function will figure out the number of bit required for any value given and return the array in the format you suggested.
I've included a loop for 0 to 20 to verify.
这通常称为位字段,您可以使用按位运算符。
That's generally referred to as a bit field, and you can work with it using bitwise operators.
这种方法被称为按位运算,在php中使用如这个。这是一个很好的教程。
This method is known as bitwise operation, and is used in php like this. Here is a nice tutorial.