Php 对拆分为4';

Php 对拆分为4';,php,for-loop,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)

我有一个输出字符串/数组,例如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)


    }
$u=16//条目总数/4为一个单位
对于($i=0;$i)
演示:

演示:


查看查看查看我的页面上有一些编辑工作要做,但效果很好!谢谢@peteryou让我的一天过得很愉快,我现在可以使用我以前不知道的功能再更改一些页面了!我的页面上有一些编辑工作要做,但效果很好!谢谢@peteryou让我的一天过得很愉快,我现在可以更改一个页面了我的页面中很少有使用此功能的页面,我对此一无所知!
$input = "010101010001001";

$result = array_map(function($i){

    return substr_count($i, "1");

}, str_split($input, 4));