多维数组未定义索引问题

时间:2019-04-09 13:34:43

标签: php multidimensional-array

我有一个正在处理的代码,无法理解如何将它忽略为空值

for ( $k = 0; $k < count( $iva_all_dpt_info[ $weekdays[ $j ] ] ); $k++ ) {
    if ( in_array( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'], $hours ) ) {

    $iva_departments[$iva_all_dpt_info[$weekdays[$j]][$k]['title']]['doctors_id'][] = isset($iva_all_dpt_info[ $weekdays[$j]][$k]['doctor_id'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors_id'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['doctor_id']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors_id'][] = '';

    $iva_departments[$iva_all_dpt_info[$weekdays[$j]][$k]['title']]['doctors'][] = isset($iva_all_dpt_info[ $weekdays[$j]][$k]['doctors'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['doctors']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors'][] = '';

    $iva_departments[$iva_all_dpt_info[$weekdays[$j]][$k]['title']]['department'][] = isset($iva_all_dpt_info[ $weekdays[$j]][$k]['department'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['department'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['department']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['department'][] = '';

    $iva_departments[$iva_all_dpt_info[$weekdays[$j]][$k]['title']]['specialty'][] = isset($iva_all_dpt_info[ $weekdays[$j]][$k]['specialty'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['specialty'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['specialty']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['specialty'][] = '';

        // if timeformat is 12 hours
        if ( $iva_timeformat == 12 ) {
            $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['hours'][] = date( 'h.i a', strtotime( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'] ) ) . ' - ' . date( 'h.i a', strtotime( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['endtime'] ) );
        } else {
            $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['hours'][]      = $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'] . ' - ' . $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['endtime'];
        }

    }
}

不确定如何为title索引使用isset。如何在其中使用isset。

2 个答案:

答案 0 :(得分:1)

使用此代码:

for ( $k = 0; $k < count( $iva_all_dpt_info[ $weekdays[ $j ] ] ); $k++ ) {
  if ( in_array( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'], $hours ) ) {
    if(isset($iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ])){
       $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors_id'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['doctor_id'];
       $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors'][] = $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['doctor'];
       $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['department'][] = $iva_all_dpt_info[ $weekdays[ $j ]][$k]['department'];
       $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['specialty'][] = $iva_all_dpt_info[ $weekdays[ $j ]][$k]['specialty'];

      // if timeformat is 12 hours
      if ( $iva_timeformat == 12 ) {
           $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['hours'][] = date( 'h.i a', strtotime( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'] ) ) . ' - ' . date( 'h.i a', strtotime( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['endtime'] ) );
      } else {
           $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['hours'][]      = $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'] . ' - ' . $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['endtime'];
      }
    }
  }
}

答案 1 :(得分:1)

我将举例说明您可以执行的操作:因此,您想检查要插入title数组的每个嵌套字段中的数据是否存在。然后,您可以做的是:

for ( $k = 0; $k < count( $iva_all_dpt_info[ $weekdays[ $j ] ] ); $k++ ) {
    if ( in_array( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'], $hours ) ) {

       isset($iva_all_dpt_info[ $weekdays[$j]][$k]['doctor_id'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors_id'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['doctor_id']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors_id'][] = '';

       isset($iva_all_dpt_info[ $weekdays[$j]][$k]['doctors'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['doctors']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['doctors'][] = '';

       isset($iva_all_dpt_info[ $weekdays[$j]][$k]['department'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['department'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['department']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['department'][] = '';

       isset($iva_all_dpt_info[ $weekdays[$j]][$k]['specialty'])?$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['specialty'][] = $iva_all_dpt_info[ $weekdays[$j]][$k]['specialty']:$iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['specialty'][] = '';

        // if timeformat is 12 hours
        if ( $iva_timeformat == 12 ) {
            $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['hours'][] = date( 'h.i a', strtotime( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'] ) ) . ' - ' . date( 'h.i a', strtotime( $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['endtime'] ) );
        } else {
            $iva_departments[ $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['title'] ]['hours'][]      = $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['starttime'] . ' - ' . $iva_all_dpt_info[ $weekdays[ $j ] ][ $k ]['endtime'];
        }

    }
}

因此,这将检查您尝试分配给字段的值是否存在,如果不存在,则将放置一个空字符串。