- 将十进制数转换为二进制数组:
$dec = 10; $bin = str_split(decbin($dec)); print_r($bin);
- 计算二进制数组中数字1的个数:
$bin = ['1', '0', '1', '0', '1']; $count = array_count_values($bin)['1']; echo $count;
- 将二进制数组转换为字符串:
$bin = ['1', '0', '1', '0', '1']; $str = implode('', $bin); echo $str;
- 反转二进制数组:
$bin = ['1', '0', '1', '0', '1']; $rev_bin = array_reverse($bin); print_r($rev_bin);
- 在二进制数组中查找特定元素:
$bin = ['1', '0', '1', '0', '1']; $key = array_search('1', $bin); echo $key;