Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array key values being emptied/zero'd immediately after being assigned

Tags:

php

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?

like image 943
fake rooted Avatar asked Apr 28 '17 11:04

fake rooted


2 Answers

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 . ' ';
      } 
    }
like image 62
delboy1978uk Avatar answered Oct 02 '22 06:10

delboy1978uk


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

like image 26
developerbmw Avatar answered Oct 02 '22 07:10

developerbmw