计算分为4的字符串中的字符

Bro*_*ina 1 php for-loop

我有一个输出字符串/数组,例如001100110011.每四个字符描绘一个单元.我想计算每个单位的1的数量.

所以对于上面我想要回报2,2,2.如果字符串是0100001100111那么它应该返回1,2,3.

我当前的脚本只计算每四个循环,所以0100001100111将返回1,3,6.

        $u = 16;//total of entries /4 is one unit


    for($i=0;$i<=$u;$i++){

    if(($i % 4) == 0){if($i==0){}else {$str .= substr_count($util_end, '1');}}

    $util_end .= $_POST['userinput'.$b];
// util_end is the input from user on a checkbox select 0 for unselected and 1 for selected in sets of four ex 0110 (two selected)


    }
Run Code Online (Sandbox Code Playgroud)

Pet*_*ter 6

$input = "010101010001001";

$result = array_map(function($i){

    return substr_count($i, "1");

}, str_split($input, 4));
Run Code Online (Sandbox Code Playgroud)

演示:http://codepad.viper-7.com/daixlB