溫馨提示×

php decbin函數(shù)的數(shù)組操作技巧有哪些

PHP
小樊
83
2024-08-10 06:19:35
欄目: 編程語言

  1. 將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制數(shù)組:
$dec = 10;
$bin = str_split(decbin($dec));
print_r($bin);
  1. 計(jì)算二進(jìn)制數(shù)組中數(shù)字1的個數(shù):
$bin = ['1', '0', '1', '0', '1'];
$count = array_count_values($bin)['1'];
echo $count;
  1. 將二進(jìn)制數(shù)組轉(zhuǎn)換為字符串:
$bin = ['1', '0', '1', '0', '1'];
$str = implode('', $bin);
echo $str;
  1. 反轉(zhuǎn)二進(jìn)制數(shù)組:
$bin = ['1', '0', '1', '0', '1'];
$rev_bin = array_reverse($bin);
print_r($rev_bin);
  1. 在二進(jìn)制數(shù)組中查找特定元素:
$bin = ['1', '0', '1', '0', '1'];
$key = array_search('1', $bin);
echo $key;

0