Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

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 ) {

about 4 years ago · Santiago Trujillo Denunciar

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 . ' ';
      } 
    }
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda