How to get the time difference corresponding to different values in php two-dimensional array

$arr=[
"0" =>["a1" => "true","time" => 1537861731],
"1" =>["a1" => "false","time" => 1541035671],
"2" =>["a1" => "true","time" => 1541036000],
"3" =>["a1" => "true","time" => 1541036010]
"4" =>["a1" => "false","time" => 1541036020]
 ];
 
 true:;
 
 false:;
 
 truefalse;
 falsetrue;
 
 
 
 :
 
 falsetrue
 
  1042
 
 
 
Php
Oct.11,2021

from your array, I feel that you are dealing strangely with the length of the call (referring to the way the array is formed).
you should be able to get the results you want with the following code:

$true_key = null;
$times = [];
foreach ($arr as $key => $value) {

    if ($value['a1'] == 'true') {
        // true
        if ($true_key === null) {
            $true_key = $key;
        }
        continue;
    }

    /**
     * 
     */
    if (($value['a1'] == 'false') && ($true_key !== null)) {
        $times[]  = $value['time'] - $arr[$true_key]['time'];
        $true_key = null;
    }
}

first take apart true and false into groups .
then traverses a group of true . And iterate over a group that is false at the same time.
each iteration advances the cursor if the current end time is not null , the end time minus the start time is the connection time.

$arr=[
    ['a1' => 'true','time' => 1537861731],
    ['a1' => 'false','time' => 1541035671],
    ['a1' => 'true','time' => 1541036000],
    ['a1' => 'true','time' => 1541036010],
    ['a1' => 'false','time' => 1541036020]
 ];

$start = [];
$finish = [];
foreach ($arr as $item) {
    if($item['a1']==='true'){
        array_push($start,$item);
        continue;
    }
    array_push($finish,$item);
}

$result = array_map(function($item)use(&$finish){
    $current = current($finish);
    next($finish);
    if($current){
        return $current['time'] - $item['time'];
    }
},$start);

var_dump($result);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b39a63-3455e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b39a63-3455e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?