Link to home
Start Free TrialLog in
Avatar of Marthaj
MarthajFlag for United States of America

asked on

Problem with an Loading/Extracting data from an Array with PHP

I am having problems with loading and extracting data in  an array.
Since  I will be using the information in various function, reading and adding to it  and so forth,.
I declared it at in the global declaration section at the beginning of my application.
I also declare a global index for it
Each filename has information associated with the filename - such  as supplier code and etc.
I can not seem to load the array correctly nor be able to extract the data for each filename.
I have tried various ways and I am unsuccessful.
I do know all the information going into the array is valid. I have verified that - I just can not seem to load/extract the data correctly.

// DEFINE GLOBAL AGENCY ARRAY AND INDEX - USED TO STUFF FILENAME INFO INTO IT

$_SESSION['GlobalAgencyIndex'] = 0;

$_SESSION['GlobalAgencyArray'] = array(
         'strAgencyFileName' => '',
         'strAgencySupplierCode'=> '',
         'strAgencyConfNbr' => '',
         'strAgencyStatus' => ''); 

Open in new window

And this is one of my functions that uses it:
Sorry it's so long.
Function RtnWrtAgencyFolder($strSupplierCode,$strConfirmNbr,$strSwitch,$strFileData)
{ 
   echo '<BR><BR>AT RtnWrtAgencyFolder';

   $strSwitch = 'AGENCY';
   $strStatus = 'NOTUPLOADED';
   
   $folder_path = $_SESSION['TmpAgencyFolder'];
   $strReplaceFolderPath = $_SESSION['TmpAgencyFolder'];
   $strAWSReplacePath = $_SESSION['AgencyAWS'];
   $strFileDataArray = $_SESSION['SavedFileData'];
   // DEFINE A TEMP ARRAY
   $index = sizeof($_SESSION['GlobalAgencyArray']);
   $strTmpCount = 0;
   echo '<BR>sizeof $_SESSION[GlobalAgencyArray]:  ' .  $index;
   
   foreach($strFileDataArray as $wrkdata)
        {
         
          $wrkTestData = trim($wrkdata['saveddata']);
         if (strLen($wrkTestData) != 0)
         {
            $wrkAWSFileName = $wrkdata['savedfilename'];
            $wrkFileName = $wrkdata['savedfilename'];
            $wrkTheSavedData  =   $wrkdata['saveddata'];
            $wrkProdFileLocation = $folder_path . $wrkFileName;
            $wrkAWSFileLocation = $_SESSION['AgencyAWS'] . $wrkFileName;
            $strDataToWrite = $folder_path . $wrkFileName;
         
            echo '<BR><BR>$wrkFileName: ' . $wrkFileName;
            echo '<BR><BR>$wrkProdFileLocation: ' . $wrkProdFileLocation;
            $_SESSION['GlobalAgencyArray'][$index] = array(
                   'strAgencyFileName' => '',
                   'strAgencySupplierCode' => '',
                   'strAgencyConfNbr' => '',
                   'strAgencyStatus' => ''); 
           
           //WRITE TO FILE
            file_put_contents($wrkProdFileLocation, $wrkTheSavedData) or die('FPUT CONTENTS FAILED');
            chmod($wrkProdFileLocation,0777);
            
            // UPDATE AGENCY INSERT ARRAY
            $strTmpNameArray = ($_SESSION['GlobalAgencyArray'][$index]);
            array_push($strTmpNameArray,$wrkFileName,$strSupplierCode,$strConfirmNbr,$strStatus);
            $index++;
            $strTmpCount = ($strTmpCount +1);
         } 
        }
      
      
        $_SESSION['GlobalAgencyIndex'] = ($_SESSION['GlobalAgencyIndex'] + $strTmpCount);
        echo'<br> $_SESSION[GlobalAgencyIndex]: '. $_SESSION['GlobalAgencyIndex'];
     
    // SANTIZE ARRAY
      $tmpWorkArray = $_SESSION['GlobalAgencyArray'];
      $_SESSION['GlobalAgencyArray'] = array_filter($tmpWorkArray);
     
      $strindex = 0;
      ECHO '<br><br>READING BACK ($_SESSION[GlobalAgencyArray]';
      $strTmp = $_SESSION['GlobalAgencyArray'];
      print_r($_SESSION['GlobalAgencyArray']);
      $strStopNbr =  $strTmpCount;
     /* 
       while($strindex <= $strStopNbr) 
      {
          echo '<BR><BR================================';
         echo '<BR>THE strAgencyFileName: ' . $_SESSION['GlobalAgencyArray'][$strindex]['strAgencyFileName'];
         echo '<BR>THE strAgencySupplierCode: ' . $_SESSION['GlobalAgencyArray'][$strindex]['strAgencySupplierCode'];
         echo '<BR>THE strAgencyConfNbr: ' . $_SESSION['GlobalAgencyArray'][$strindex]['strAgencyConfNbr'];
         echo '<BR>THE strAgencyStatus: ' . $_SESSION['GlobalAgencyArray'][$strindex]['strAgencyStatus'];
            $strindex ++;
       }
      */    
  
}

Open in new window

I know this can be done, I just haven't been able to get  the coding right.
And I know I will have to update its index after a function has added to it.
But I think that would be just a matter of updating as I load the array.
Any help would be appreciated. It's urgent.
Thank you in advance.

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

I haven't looked at your code in depth so I'm not sure why you're using the session to store the data. However, if that is intentional, make sure you are calling session_start() at the beginning of each page load - BEFORE any other content output or whitespace. If you don't call it, it won't persist the session data across different page loads.
Avatar of Marthaj

ASKER

Thank you for responding.
This is what I have at the beginning of my application:
if(!session_id()){ session_start();}

Open in new window

Which should work - right ? I did replace the array _push with old way:
Replaced this:
 array_push($strTmpNameArray,$wrkFileName,$strSupplierCode,$strConfirmNbr,$strStatus)

With this:

           $_SESSION['GlobalAgencyIndex'] = ($_SESSION['GlobalAgencyIndex'] + 1);
           $strTmpArray[$strIndex]['strAgentFileName'] = $wrkFileName;
           $strTmpArray[$strIndex]['strAgencySupplierCode'] = $strSupplierCode;
           $strTmpArray[$strIndex]['strAgencyConfNbr'] = $strConfirmNbr;
           $strTmpArray[$strIndex]['strAgencyStatus'] = $strStatus;

Open in new window

And that helped a great deal. But why can't I use array_push ??
I have used it before with arrays but never with one defined as a $_SESSION declaration.
I originally had my code this way, and the array seemed to 'lose' the data. I only will be using it
in one other function (which I haven't even written).And because of the flow of the program, it will not always use this function.
For Example, I read a email and it might go to this function.
Or the email might be routed to another function  that doesn't even use it.
All I know is that at the end of reading all the incoming mail, I need the information in this array
to do some updating functions....

No, you just do session_start() each time. Don't wrap it in an if() condition.

The session_start() function has a confusing name because it starts a new session if it doesn't exist, AND resumes a session if it does exist.


Hello,
For the sake of clarity, I am going to use javascript/json syntax so that you can better understand the problem.  Read through the discussion first.  At the end you will find the correct PHP syntax you should use.

On line 5 of your first block of code you are NOT initializing the session variable to an actual array; you basically have the following (again, in javascript/json syntax):
// here you are initializing your session variable to an object,
// (associative array in php) which is probably NOT what you intended.
$_SESSION['GlobalAgencyArray'] = { 'strAgencyFileName':''
                                  ,'strAgencySupplierCode':''
                                  ,'strAgencyConfNbr':''
                                  ,'strAgencyStatus':''
                                 };
// if you now inspect its size you get 4 because it has four properties:
echo count($_SESSION['GlobalAgencyArray']);

// notice however, that it does not have an element with index zero:
echo $_SESSION['GlobalAgencyArray'][0];//error

Open in new window

I suspect that you intended those four properties to be saved as a "group" of properties belonging to one record.  If that is the case, what you needed to do is essentially:
// noticed he square brackets
$_SESSION['GlobalAgencyArray'] = [{ 'strAgencyFileName':''
                                  ,'strAgencySupplierCode':''
                                  ,'strAgencyConfNbr':''
                                  ,'strAgencyStatus':''
                                 }];
// now all those properties are saved under index zero:
print_r($_SESSION['GlobalAgencyArray'][0]);//OK

Open in new window


So, the correct way to initialize that session array is by using an array of associative arrays:
$_SESSION['GlobalAgencyArray'] = array( array('strAgencyFileName' => '',
                                              'strAgencySupplierCode'=> '',
                                              'strAgencyConfNbr' => '',
                                              'strAgencyStatus' => '')
                                  );

Open in new window

As for "why can't I use array_push ??", the reason it does not work is because it appends elements to the end of an existing array.  From what I see, you are actually trying to overwrite all the existing property values.  Typically, if you wanted to overwrite some (but not ALL) of the properties, you would use array_merge() instead, but in your case you can simply do a complete assignment since you are overwriting all the existing properies.  So what you need to use is:
...
            // UPDATE AGENCY INSERT ARRAY
            //$strTmpNameArray = ($_SESSION['GlobalAgencyArray'][$index]);
            //array_push($strTmpNameArray, $wrkFileName, $strSupplierCode, $strConfirmNbr, $strStatus);

         $_SESSION['GlobalAgencyArray'][$index] = Array('strAgencyFileName'=>$wrkFileName,'strAgencySupplierCode'=>$strSupplierCode,'strAgencyConfNbr'=>$strConfirmNbr,'strAgencyStatus'=>$strStatus);

...

Open in new window

Regards,
hielo

Avatar of Marthaj

ASKER

Thank you for responding. It certainly has helped. And excellent explanation too.  Yes, you are correct. I want the 4, strAgencyFileName,strAgencySupplierCode,strAgencySupplierCode,strAgencyConfNbr,strAgencyStatus, to be as one record so to speak.  And I will be consistently  adding to this array . populating all 4 values, in various functions thru out the program. needing to preserve the array till the very end of processing.
And do not want to overwrite any of the entries. I just want add to the array and be able to read it back to verity the testing results.
 So, then, do I understand it correctly that the I need to add brackets to my session var  $_SESSION['GlobalAgencyArray']
to achieve this 'record-like' array?  And then when  I want to add an 'record' so to speak, so then would the line(s) to add another row would be this? Or does it then allow the use of the  push_array?

// to add to array
 $strTmpArray[$strIndex]['strAgentFileName'] = $wrkFileName;
  
and so forth for each of the other values.

Open in new window

And using an associate array -  means exactly what ??
Thank you for your patience, I don't have much experience with them in PHP, but, I am learning..
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marthaj

ASKER

I understand what you mean by changing my mindset, I don't always know how to explain what I mean.
I have always thought of arrays like a record description that we used in COBOL. And to me, they are just like rows in a database table, each column may/may not have a value.
 I do need to have the right mindset in order to understand..
I am not quite sure I understand about associative arrays.  
There are actually two arrays defined as $_sessions for same reason but with different name/data. But I am not going to muddy the waters here.
If I understand you correctly....
f I leave the array definition as this:
$_SESSION['GlobalAgencyArray'] = array(
         'strAgencyFileName' => '',
         'strAgencySupplierCode'=> '',
         'strAgencyConfNbr' => '',
         'strAgencyStatus' => ''); 

Open in new window

Then adding data to array would be this ??
// ASSIGN VALUES TO VARIABLES
    $wrkFileName = some value;
    $strSupplierCode = some value;
    $strConfirmNbr = some value;
    $strStatus some value;

array_push($_SESSION['GlobalAgencyArray'],("strAgencyFileName"=> $wrkFileName,       
         "strAgencySupplierCode"=>$strSupplierCode,"strAgencyConfNbr"=>  
          $strConfirmNbr,"strAgencyStatus" => $strStatus));
Does it matter if I use double or single quotes ??
                             

Open in new window

Then to display them back would be: ??
// SANTIZE ARRAY
$tmpWorkArray =  $_SESSION['GlobalAgencyArray'];
$_SESSION['GlobalAgencyArray'] = array_filter($tmpWorkArray);

$strRecordNbr = 0;
echo '<BR><BR======DISPLAY ARRAY  =============';

foreach($strTmpArray as $strValues) 
   {
      echo '<BR><BR====== INDIVIUAL ENTRY ================';
      echo '<BR> $strRecordNbr: ' . $strRecordNbr;
      echo '<BR>$strValues: ' . $strValues['strAgencyFileName'];
      echo '<BR>$strValues: ' . $strValues['strAgencySupplierCode'];
      echo '<BR>$strValues: ' . $strValues['strAgencyConfNbr'];
      echo '<BR>$strValues: ' . $strValues['strAgencyStatus'];
      $strRecordNbr =  ($strRecordNbr + 1);   
   }

Open in new window

Associate Array:
If I defined it as an associative array then adding data would be this ?

$_SESSION['GlobalAgencyArray'] = array(array(
         'strAgencyFileName' => '',
         'strAgencySupplierCode'=> '',
         'strAgencyConfNbr' => '',
         'strAgencyStatus' => ''); 

// ASSIGN VALUES TO VARIABLES
    $wrkFileName = some value;
    $strSupplierCode = some value;
    $strConfirmNbr = some value;
    $strStatus some value;

This would be to add data to array ??

$strDataToAdd = ($wrkFilenane . $strSupplierCode . $strConfirmNbr . $strStatus);
array_push( $_SESSION['GlobalAgencyArray'],$strDataToAdd);

Then this is how to display it back ??

// SANTIZE ARRAY
$tmpWorkArray =  $_SESSION['GlobalAgencyArray'];

$_SESSION['GlobalAgencyArray'] = array_filter($tmpWorkArray);
$strTmpArray = $_SESSION['GlobalAgencyArray'];
$strRecordNbr = 0;

 echo '<BR><BR======DISPLAY ARRAY  =============';

 foreach($strTmpArray as $strValues) 
   {
           echo '<BR><BR====== INDIVIUAL ENTRY ================';
      echo '<BR> $strRecordNbr: ' . $strRecordNbr;
      echo '<BR>$strValues: ' . $strValues['strAgencyFileName'];
      echo '<BR>$strValues: ' . $strValues['$strAgencySupplierCode'];;
      echo '<BR>$strValues: ' . $strValues['strAgencyConfNbr'];
      echo '<BR>$strValues: ' . $strValues['strAgencyStatus'];
      $strRecordNbr =  ($strRecordNbr + 1);
   }

OR would it be 
 foreach($strTmpArray as $strValues) 
   {
           echo '<BR><BR====== INDIVIUAL ENTRY ================';
      echo '<BR>Values: ' . $strValues;
      $strRecordNbr =  ($strRecordNbr + 1);
   }

If the above is true, then how do I break it into individual values again ??
This way ? 

 foreach($strTmpArray as $strValues) 
   {
      echo '<BR><BR====== INDIVIUAL ENTRY ================';
      echo '<BR>Values: ' . $strValues[0];
      echo '<BR>Values: ' . $strValues[1];
      echo '<BR>Values: ' . $strValues[2];
      echo '<BR>Values: ' . $strValues[3];
      $strRecordNbr =  ($strRecordNbr + 1);
   }

Open in new window

Thank you for being patience.
Avatar of Marthaj

ASKER

Thank you both again for responding. Looking at the most recent coding, I can see that I missed the mark. Pretty much so.
Hielo  - I am going to copy your explanation, (and a very good one too) and study upon it. - I keep waiting for that AHA moment. Something isn't clicking for me about it. :(
> And to me, they are just like rows in a database table, each column may/may not have a value.
Exactly! A table is two-dimensional by nature - each cell has both a row AND a column index.  So, if you were dealing with a chess board and wanted to reference the white knight on B1, and your chessboard were modeled as a 2D indexed array, you would dereference it as square[0][1] (row=0;column=1).
Which is why if you take another look at the way you initialized the array, you provided only the column indices.  For the sake of clarity I am going to assign them actual cell values 'A'-'D':
$_SESSION['GlobalAgencyArray'] = array('strAgencyFileName' => 'A'
                                      ,'strAgencySupplierCode'=> 'B'
                                      ,'strAgencyConfNbr' => 'C'
                                      ,'strAgencyStatus' => 'D'
                                      );

Open in new window


If instead of using an associative array you had decided to use an indexed array, the above would simply be:
$_SESSION['GlobalAgencyArray'] = array('A'
                                      ,'B'
                                      ,'C'
                                      ,'D'
                                      );

Open in new window

but that does not make $_SESSION['GlobalAgencyArray'] a "table" of records; it's just a variable that points to an array that contains four values.  This is obvious when you try to extract the values - 
echo $_SESSION['GlobalAgencyArray'][2]; //will show "B"

// but the following is an error because GlobalAgencyArray is NOT a 2D array
// (it is not an accurate representation of a DB table)
// echo $_SESSION['GlobalAgencyArray'][0][2];

Open in new window

By contrast, if you initialize it as:
$_SESSION['GlobalAgencyArray'] = array( array('A'
                                              ,'B'
                                              ,'C'
                                              ,'D'
                                              )
                                 );

Open in new window

what is happening is that the outer "array()" sees ONE item/value, which just happens to be an array.  So:
// $_SESSION['GlobalAgencyArray'][0] is a reference to the inner array
// thus the following should reveal the inner array
echo '<pre>', print_r($_SESSION['GlobalAgencyArray'][0], true), '</pre>';

// and if you want to see "B", you would need:
echo $_SESSION['GlobalAgencyArray'][0][1];

Open in new window

Now the only thing left to understand is that in your case, because the inner array is an associative array, you need to use the corresponding string key to dereference the column index.  So, to get "B", instead of [0][1] you would use [0]['strAgencySupplierCode'].  The advantage of the associative array is that the order in which the values are stored do not matter.
So if at deployment time you have:
$_SESSION['GlobalAgencyArray'] = array(array('strAgencyFileName' => 'A'
                                      ,'strAgencySupplierCode'=> 'B'
                                      ,'strAgencyConfNbr' => 'C'
                                      ,'strAgencyStatus' => 'D'
                                      ));
// the following will reveal "B"
echo $_SESSION['GlobalAgencyArray'][0]['strAgencySupplierCode'];

Open in new window

and three months later you decide to add a new property, the echo line still shows the correct value:
$_SESSION['GlobalAgencyArray'] = array(array('strAgencyFileName' => 'A'
                                      ,'intMaxEmployees'=>100
                                      ,'strAgencySupplierCode'=> 'B'
                                      ,'strAgencyConfNbr' => 'C'
                                      ,'strAgencyStatus' => 'D'
                                      ));
// the following will still reveal "B"
echo $_SESSION['GlobalAgencyArray'][0]['strAgencySupplierCode'];

Open in new window

Notice however that if you had used an indexed array and inserted the new property in between the first two, then you would no longer get "B":
// remember, if we had used indexed arrays at deployment time, 
// the '100' would not exist and the echo that follows was 
// intended to extract the "B".  When you come back to your code
// three months later and add that '100', it throws off your old index 
// references by one, starting at the index where you added the value.
$_SESSION['GlobalAgencyArray'] = array(array( 'A'
                                              ,100
                                              ,'B'
                                              ,'C'
                                              ,'D'
                                            ));
// originally, $_SESSION['GlobalAgencyArray'][0][1] would give you "B"
// but now, you will get '100' instead.
echo $_SESSION['GlobalAgencyArray'][0][1];

Open in new window

So, to summarize, you were using 'GlobalAgencyArray' to store a single row, when you actually needed table (a 2D array). Hopefully this got you to your "aha" moment.

Regards,
Hielo