1

So I would like to use a CSV file as a array.

This is part of the huge CSV file I have.

1-1,Poh,ਪੋਹ,19
1-2,Poh,ਪੋਹ,20
1-3,Poh,ਪੋਹ,21
1-4,Poh,ਪੋਹ,22
1-5,Poh,ਪੋਹ,23
1-6,Poh,ਪੋਹ,24

So in PHP, I would call for 1-4. How can I call 1-4, and then get the output of the 3 values after it, in this case it is Poh,ਪੋਹ,22.

Then I make it so $1 = Poh, $2 = ਪੋਹ, $3 = 22.

How would I go doing this?

3
  • Have you tried any code? What error did you encounter? Commented Jan 18, 2016 at 2:47
  • php.net/manual/en/function.str-getcsv.php Commented Jan 18, 2016 at 2:50
  • @Matt, what do I do, I already tried that Commented Jan 18, 2016 at 2:51

1 Answer 1

2

You can do it like this:

$lines = file('.../xx.csv');
foreach ($lines as $raw_line) {
  $line = explode(',', $raw_line);
  $csv[$line[0]] = [$line[1], $line[2], $line[3]];
}
// then you can access to:
$csv['1-4'][0] // --> Poh
$csv['1-4'][1] // --> ਪੋਹ
$csv['1-4'][2] // --> 22
Sign up to request clarification or add additional context in comments.

10 Comments

I was thinking the exact same thing, but it may cause issues if he has comma values within the columns that are escaped but by the looks of it, that may not be an issue.
@Matt Thanks for correcting my typo. Regarding the commas, I agree. Anyway, since this comes from CSV, if commas can be included, the OP will be forced to use delimited mode, and yes, this code should be revised accordingly.
@cFreed Where is $csv defined?
@SarabveerSingh $csv is populated at line 3 in the above code.
@cFreed Parse error: syntax error, unexpected '$csv' (T_VARIABLE)
|

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.