Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
array key values being emptied/zero'd immediately after being assigned

I have a form that sets up the page's input field values using PHP like:

// create and fill inputVarsArr with keys that have empty vals
$troubleInsertInputVarsArr = array_fill_keys(array('status', 'discoverDate', 'resolveDate', 'ticketIssued', 'eqptSystem', 'alarmCode', 'troubleDescription', 'troubleshootingSteps', 'resolveActivities', 'notes'), '');

if (isset ($_POST['insert'])) { // update DB site details 
    foreach ($troubleInsertInputVarsArr as $key => $val) {
        if ($key !== 'discoverDate' && $key !== 'resolveDate') { //deal with dates separately below
            $val = $_SESSION[$key] = sanitizeText($_POST[$key]);
            echo '<br>(' . __LINE__ . ') ' . $key . ': ' . $val . ' ';
        } // close IF ; line 47 echo
    } // close FOREACH

    foreach ($troubleInsertInputVarsArr as $key => $val) {
        echo '<br>(' . __LINE__ . ') ' . $key . ': ' . $val . ' ';
    } // close FOREACH // line 52 echo
}

The input vars echo'd in the if conditional nested in the foreach loop (echo' on line 47) print out as I expect (values entered into the form page's input fields):

(47) status: Outstanding
(47) ticketIssued: no
(47) eqptSystem: Tower Lights
(47) alarmCode: Visual
(47) troubleDescription: - no description yet -
(47) troubleshootingSteps: - no steps yet taken -
(47) resolveActivities: - no activities yet decided -
(47) notes: - no notes -

But then the same key-val pairs in the same array echo'd in the foreach loop that immediately follows (echo'd on line 52), without any further variable processing having occurred, prints out empty values for each key:

(52) status:
(52) discoverDate:
(52) resolveDate:
(52) ticketIssued:
(52) eqptSystem:
(52) alarmCode:
(52) troubleDescription:
(52) troubleshootingSteps:
(52) resolveActivities:
(52) notes: 

Somehow the array's key values are being zero'd out right after they are assigned and I can't figured out how or why. Can anyone see an obvious reason why this might be occurring?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The $val = $_SESSION[ $key ] = sanitizeText( $_POST[ $key ] ); line does not change the $troubleInsertInputVarsArr array as $val is a copy of the array element value, not a reference.

Change the foreach so that $val is a reference.

foreach ( $troubleInsertInputVarsArr as $key => &$val ) {

over 4 years ago · Santiago Trujillo Relatório

0

You never assign values to $troubleInsertInputVarsArr, only keys. You could try this:

   foreach ( $troubleInsertInputVarsArr as $key => $val ) {
      $troubleInsertInputVarsArr[$key] = sanitizeText( $_POST[ $key ]); 
      if ( $key !== 'discoverDate' && $key !== 'resolveDate' ) { 
          $val = $_SESSION[ $key ] = sanitizeText( $_POST[ $key ] );
          echo '<br>(' . __LINE__ . ') ' . $key . ': ' . $val . ' ';
      } 
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda