PHP

php decbin函数的数组操作技巧有哪些

小樊
83
2024-08-10 06:19:35
栏目: 编程语言

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

0
看了该问题的人还看了