PHPでビット配列を作る

配列の要素はtrueもしくはfalse。

<?php

function makeBitArray($binary)
{
    if (!is_string($binary)) throw new Exception;
    
    $bitarray = array();
    foreach (str_split($binary) as $char) {
        list(, $char) = unpack('C', $char);
        $arr = array();
        for ($i = 0; $i < 8; $i++) {
            $arr[] = ($char & 1) ? true : false;
            $char >>= 1;
        }
        $bitarray = array_merge($bitarray, array_reverse($arr));
    }
    return $bitarray;
}

foreach (makeBitArray("hoge") as $bit) echo $bit ? 1 : 0 ;

実行結果

01101000011011110110011101100101