代码之家  ›  专栏  ›  技术社区  ›  Matt Welander

如何访问嵌套关联数组中的密钥

  •  0
  • Matt Welander  · 技术社区  · 3 年前
    public function getEntriesArray($sort = 'asc', $studentEntries = false)
    {
        if ($studentEntries) {
            $entries = $this->getStudentEntries();    
        } else {
            $entries = $this->getEntries();    
        }
    
        $arrayOfEntries = array();
        $ojti_totals = array();
    
        foreach ($entries as $e) {
            $a = array();
            $ojti_totals = array();
    
            $a['id'] = $e->getId();
            
            $a['start_unix'] = $e->getStart()->format('U');
            $a['start_date'] = $e->getStart()->format('Y-m-d');
            $a['start_time'] = $e->getStart()->format('h:i');
    
            $a['stop_unix'] = $e->getStop()->format('U');
            $a['stop_date'] = $e->getStop()->format('Y-m-d');
            $a['stop_time'] = $e->getStop()->format('h:i');
    
            $a['verified'] = $e->getVerifiedAsCorrect();
    
            $a['position_id'] = $e->getPosition()->getId();
            $a['position_name'] = $e->getPosition()->getName();
    
            $a['duration'] = $e->getDuration();
            $a['duration_time_human_short'] = $e->getDuration('time_human_short');
    
            if ($studentEntries) {
                $a['ojti_id'] = $e->getOperator()->getId();
                $a['ojti_signature'] = $e->getOperator()->getSignature();
    
                //Not the nicest implementation, kind of threw this in afterward. Some duplication going on since this is now stored on every entry, and the last one holds the ackumulated total.
                $ojti_totals[ $a['ojti_id'] ]['signature'] = $a['ojti_signature'];
    
    //problem starts here:
    
                if ($ojti_totals[ $a['ojti_id'] ]['duration']) {                
                    $ojti_totals[ $a['ojti_id'] ]['duration'] = $ojti_totals[ $a['ojti_id'] ]['duration'] + $a['duration']/3600;                    
                } else {
                    $ojti_totals[ $a['ojti_id'] ]['duration'] = $a['duration']/3600;                                        
                }
    
                $a['ojti_totals'] = $ojti_totals;
    
            } else {
                if ($e->getStudent()) {
                    $a['student_id'] = $e->getStudent()->getId();
                    $a['student_signature'] = $e->getStudent()->getSignature();
                } else {
                    $a['student_id'] = null;
                    $a['student_signature'] = '-';            
                }                
            }
    
            $arrayOfEntries[] = $a;
        }
    

    目的是设定 $ojti_totals[ $a['ojti_id'] ]['duration'] 因此,对于每次迭代,应增加持续时间的现有值。

    但我找不到进入的方法 $ojti_总计[$a['ojti_id']['duration'] .

    上面的代码导致

    注意:未定义索引:持续时间

    改成

    if (isset($ojti_totals[ $a['ojti_id'] ]['duration']) {
    

    永远不可能是真的。也不是

    if (array_key_exists('duration',$ojti_totals[ $a['ojti_id'] ])) {  
    

    5次迭代后阵列的var_转储

    array(1) {
      [12]=>
      array(2) {
        ["signature"]=>
        string(2) "CN"
        ["duration"]=>
        float(2.2477777777778)
      }
    }
    array(1) {
      [12]=>
      array(2) {
        ["signature"]=>
        string(2) "CN"
        ["duration"]=>
        float(0.33333333333333)
      }
    }
    array(1) {
      [12]=>
      array(2) {
        ["signature"]=>
        string(2) "CN"
        ["duration"]=>
        float(0.75)
      }
    }
    array(1) {
      [12]=>
      array(2) {
        ["signature"]=>
        string(2) "CN"
        ["duration"]=>
        float(0.96666666666667)
      }
    }
    array(1) {
      [12]=>
      array(2) {
        ["signature"]=>
        string(2) "CN"
        ["duration"]=>
        float(2.2477777777778)
      }
    }
    
              
    
    0 回复  |  直到 3 年前