如何才能获得最大连续+值?

There are a lot values in $variable which has randomly -ve and +ve values. How can I COUNT max consecutive positive value? for example,

 $variable = array:12 [▼
              0 => 258300
              1 => 668000
              2 => -1510530
              3 => 15000
              4 => 2400
              5 => 13400
              6 => 284000
              7 => -45000000
              8 => 7209702
              9 => 1000074080
              10 => 0
              11 => 1100
            ]

In the above case, Output should look like: 4 has max constructiveness of positive value. because,

              3 => 15000
              4 => 2400
              5 => 13400
              6 => 284000

These values has continue +ve value and its max consecutive.

  • Create some variables to store some temp values
  • Loop over the $variable, and determine if it is in positive trend or not. Positive trend means previous and current value are both positive.
  • Store it in the current Positive trend, and check if your previous max consecutive trend is breached or not. If it is breached, you can set the current trend as the new max trend.

Try the following code:

// Define a variable which stores the previous value
$prev = null;
$current_positive_trend = array();
$max_consecutive_positive_trends = array();

foreach ($variable as $value) {

    /* For PHP 5 you can do the following instead
    $prev = isset($prev) ? $prev : $value;
    */
    $prev = ($prev ?? $value); // first time consider current value

    // if current value and previous value is greater than zero
    if ($prev > 0 && $value > 0) {

        // add the value to current positive trend
        $current_positive_trend[] = $value;

        // check if the count of current positive trend array is more than
        // the max consecutive positive trends found till now
        if (count($current_positive_trend) > count($max_consecutive_positive_trends)) {

            // set current trend to max consecutive trends
            $max_consecutive_positive_trends = $current_positive_trend;
        }
    } else {

        // not in positive trend - reset
        $current_positive_trend = array();
    }
}

$max_times_consecutive_positive = count($max_consecutive_positive_trends);

// print the consecutive positive values (in max case
var_dump($max_consecutive_positive_trends);
// print the max times trend was positive.
echo $max_times_consecutive_positive;

Did you want it done with minimal code?

<?
$v = [
    258300,
    668000,
    -1510530,
    15000,
    2400,
    13400,
    284000,
    7209702,
    1000074080,
    0,
    1100,
    45,
    -1
];
$m=0;
$c=0;

foreach ($v as $i) {$c=($i>0)?((++$c>$m)?($m=$c):$c):0;};

var_dump($m);

Here is the code. you should optimize the code. i have add some comments that help you for additional changes

you can check your desired output here

    <?php

    $data=array(258300,668000,-1510530,15000,2400,13400,284000,-45000000,7209702,1000074080,0,1100);
    $bak_index=array();
    $bak=array();
    $index_start=0;
    // print_r(count($data));
    // exit;
    for($i=0;$i<count($data);$i++){


        if($data[$i] <=0){
            //echo $i.'='.$data[$i].'__'; for testing
            $index_end=$i;
            $bak_index['start']=$index_start;//get start index
            $bak_index['end']=$index_end;//get end index number
            $bak_index['total']=$bak_index['end']-$bak_index['start']; //total positive number index
            $index_start=$i+1;
            $bak[]=$bak_index;
        }


    }
    echo "<pre>";
    print_r($bak);//here is array with start index, end index & total  positive consecutive value
    $max_value=max(array_column($bak, 'total'));
    //print_r($max_value);
 //now get values from $data varaible using array_slice function   
    for ($i=0; $i < count($bak) ; $i++) {
        if($bak[$i]['total'] == $max_value){
            echo $bak[$i]['start'];
            echo "_____";
            echo $bak[$i]['end'];
            echo "<br>";
            $value[]= array_slice($data, $bak[$i]['start'], $bak[$i]['total']);
        }

    }
    print_r($value);