Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:41:27+00:00 2026-06-16T01:41:27+00:00

I would like the following code to assign a variable to each value read

  • 0

I would like the following code to assign a variable to each value read from my CSV file.

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

So for example, inside the CSV file I have 4 values from 2 Excel columns:

Jack,31/01/1991
Ross,15/03/1989

And I would like the make Jack variable $vrble1, 31/01/1991 $vrble2, Ross $vrble3 and 15/03/1989 $vrble4.

How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:41:28+00:00Added an answer on June 16, 2026 at 1:41 am

    Per each iteration you merge the data into a single array:

    $file = new SplFileObject("test.csv");
    $file->setFlags(SplFileObject::READ_CSV);
    $data = call_user_func_array('array_merge', iterator_to_array($file));
    

    And then you can extract the array data into the local varialbles like this:

    $data = array_combine(range(1, count($data)), $data);
    extract($data, EXTR_PREFIX_ALL, 'vrble');
    

    See array_combine and extract.

    However you should continue using arrays if you ask me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in HTML, and I would like to retrieve name12@host.com from
In the following code I would like to hide Hello world but keep visible
I would like to optimize the following code in R. This loop takes a
I would like to know if the following code are valid. The original intension
I would like to know if the following code would be a good pattern
I would like to know if it is safe to use the following code
I'm using the following code successfully with HTTP but I would like to use
I have the following code which works perfectly fine, however I would like to
I have the following code, which works as I expect. What I would like
I have a unicode filename that I would like to open. The following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.