~~~
function extract_binary($input) {
//数字转二进制字符串
$string = decbin($input);
//翻转字符串
$string = strrev($string);
//字符串拆成数组
$values = str_split($string);
//过滤掉数组中的0,只剩下1,元素索引不变
$values = array_filter($values, function($value) {
return $value;
});
//把剩下索引取出
$keys = array_keys($values);
//索引是几,就是2的几次方
$segments = [];
foreach ($keys as $k) {
$segments[] = pow(2, $k);
}
return $segments;
}
$value = 1+2+8+32+128+512;
$values = extract_binary($value);
var_dump($values);
~~~