0

I am having some troubles finding a solution after researching for hours. I have one column of data called LAST4 in my database. Some data examples "0564", "0002", etc... When I run the script, it removes the leading zero of "0564" and turns it into "564" or "0002" results to "2". How can I save the data with the leading zeros? Any help would be appreciated!

    //CSV Database 
    $handle = fopen("/home/file/path.csv", "r");

    $count = 0;
    $members = array();

    while(($data = fgetcsv($handle, 10000, ",")) !== FALSE) {

      if ($count >= 1) { 

         /* Convert Date of Birth to Timestamp with Function */   
         $dateofbirth = date("Y-m-d", strtotime($data[2]));  

         /* Convert Layoff Date to Timestamp with Function */   
         $layoffdate = date("Y-m-d H:i:s", strtotime($data[5])); 


        /* Build Variables for User Access Account */
        $userlogin = $dateofbirth . '-' . $data[3];
        $userpw = $data[1] . '' . $data[3];
        $userem = $dateofbirth . '-' . $data[3] . '@example.org';

        /* Creates a New User For User Access */
         $userDatas[] = array(
          'user_login' => $userlogin,
          'first_name' => $data[0],
          'last_name' => $data[1],
          'user_pass' => $userpw,
          'user_email' => $userem,
          'user_url' => '',
          'role' => 'member'
         );

         /* Creates New Member Entry in Table */
         $members[] = array(  
         'FIRST_NAME' => $data[0],
         'LAST_NAME' => $data[1],
         'DOB' => $dateofbirth, 
         'LAST4' => $data[3],
         'FLAG' => $data[4],
         'LAID_OFF_DATE' => $layoffdate, 
         'PHONE_NUMBER' => $data[6]
         ); 

       }

       $count++;

1 Answer 1

1

Try casting it to string first. So instead of:

'LAST4' => $data[3],

You could use:

'LAST4' => (string)$data[3],

And similarly with other values you'd want it to happen with.

Sign up to request clarification or add additional context in comments.

2 Comments

What puzzles me about this one is why is the column is being implicitly cast to numeric to begin with, I don't see any obvious mathematical operations that would be causing that to happen.
@ArchCodeMonkey Welp, me neither, otherwise I would've given a short explanation on why this is happening. I kinda hoped noone would bring it up. My best guess is simply "it do be like that sometimes".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.