Проблема многомерного массива с неопределенным индексом - PullRequest
0 голосов
/ 09 апреля 2019

У меня есть код, над которым я работаю и не могу понять, как мне его обойти для нулевого значения

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'];
        }

    }
}

Не уверен, как использовать isset для индекса title.Как я могу использовать isset в нем.

Ответы [ 2 ]

1 голос
/ 09 апреля 2019

Я приведу пример того, что вы можете сделать: Итак, вы хотите проверить, существуют ли данные, которые вы хотите вставить в каждое из вложенных полей массива 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'];
        }

    }
}

Так что это проверит, существует ли значение, которое вы пытаетесь присвоить вашему полю, и если нет, то просто поместит пустую строку.

1 голос
/ 09 апреля 2019

Используйте этот код:

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'];
      }
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...