skip to Main Content
Array
(
    [0] => Array
        (
            [0] => OLAO01
            [1] => OLAO01
            [2] => BARR04
            [3] => OLAO01
            [4] => MADD03
        )

    [1] => Array
        (
            [0] => BARR04
            [1] => POWE03
        )

    [2] => Array
        (
            [0] => BARR04
            [1] => COLL09
        )

    [3] => Array
        (
            [0] => BARR04
            [1] => SARC01
            [2] => OLAO01
        )

    [4] => Array
        (
            [0] => BARR04
            [1] => HORS02
        )

    [5] => Array
        (
            [0] => BARR04
        )

    [6] => Array
        (
            [0] => OLAO01
        )

    [7] => Array
        (
            [0] => RYDE02
        )

    [9] => Array
        (
            [0] => CAMP02
        )

    [10] => Array
        (
            [0] => WRIG05
            [1] => CAMP02
            [2] => LEMO01
        )

    [11] => Array
        (
            [0] => OLAO01
        )

    [12] => Array
        (
            [0] => HIPP01
            [1] => LEMO01
        )
)

I have array like this I needs to find if how many time one value repeating consecutive BARR04

Repeated 6 times. But if one array not that value then for that counter it will set to 1 again

$result = array_intersect($newary[$i], $newary[$i+1]);  

I tried with this but not working properly

2

Answers


  1. Just iterate through the subarrays and keep track of the consecutive occurrences of "BARR04":

    $arr = [
        ["OLAO01", "OLAO01", "BARR04", "OLAO01", "MADD03"],
        ["BARR04", "POWE03"],
        ["BARR04", "COLL09"],
        ["BARR04", "SARC01", "OLAO01"],
        ["BARR04", "HORS02"],
        ["BARR04"],
        ["OLAO01"],
        ["RYDE02"],
        ["CAMP02"],
        ["WRIG05", "CAMP02", "LEMO01"],
        ["OLAO01"],
        ["HIPP01", "LEMO01"]
    ];
    
    $target = "BARR04";
    $consecutiveCount = 0;
    $maxConsecutiveCount = 0;
    
    foreach ($arr as $subarr) {
        if (in_array($target, $subarr)) {
            // if the value is present in the subarray, update the count
            $consecutiveCount++;
            if ($consecutiveCount > $maxConsecutiveCount) {
                $maxConsecutiveCount = $consecutiveCount;
            }
        } else {
            // if the value is not present the streak is broken and you reset the count
            $consecutiveCount = 0;
        }
    }
    
    echo "The value '$target' repeats consecutively $maxConsecutiveCount times.";
    
    Login or Signup to reply.
  2. You can flatten the array and count how many times a value occurs. Like this:

    $array = [
        ["OLAO01", "OLAO01", "BARR04", "OLAO01", "MADD03"],
        ["BARR04", "POWE03"],
        ["BARR04", "COLL09"],
        ["BARR04", "SARC01", "OLAO01"],
        ["BARR04", "HORS02"],
        ["BARR04"],
        ["OLAO01"],
        ["RYDE02"],
        ["CAMP02"],
        ["WRIG05", "CAMP02", "LEMO01"],
        ["OLAO01"],
        ["HIPP01", "LEMO01"]
    ];
    
    function getValueCount($array, $value){
        $flattened = array_merge(...$array);
        $count = array_count_values($flattened);
        return $count[$value] ?? 0;
    }
    

    Use like this:

    echo getValueCount($array, "BARR04");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search