Get the value of only the first entry in $data[1] of a CSV file

Hi Guys,
How can I get the value of just the very first entry in $data[1] of a CSV file that has many values?
I don't want to get the value of the other entries in $data[1].
I just want to isolate the first value.
The attempts in the code below are logical to me, but do not function as desired.
Thanks,
Sas
	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE  ) {
	if ($data[1]=='ExactlyThis'){$doSomething='Do This';}else{$doSomething='Do Nothing';}
	if (strpos($data[1], 'ExactlyThis')){$doSomething='Do This';}else{$doSomething='Do Nothing';}

Open in new window

LVL 1
sasnaktivAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Easy once we can see the data!

Please see these links:
http://iconoun.com/demo/temp_sasnaktiv.php?q=1
http://iconoun.com/demo/temp_sasnaktiv.php?q=2

Here is the test data:
http://iconoun.com/demo/temp_sas1.csv
http://iconoun.com/demo/temp_sas2.csv

Here is the program code:
<?php // demo/temp_sasnaktiv.php
error_reporting(E_ALL);

// CHOOSE FILE ONE OR TWO
$n = !empty($_GET['q']) ? $_GET['q'] : '0';
$f = 'temp_sas' . $n . '.csv';

// GET THE FIRST RECORD AND EXTRACT THE SIGNAL STRING
$fpr = fopen($f, 'r');
$csv = fgetcsv($fpr);
$sig = $csv[0];


// SHOW THE FIRST POSITION
var_dump($sig);

Open in new window

0
 
Ray PaseurCommented:
PHP fgetcsv() returns an array.  Arrays are numbered from zero, not from 1.  So if you want the first value you might want to look at $data[0].

You can print out the contents of a PHP array with var_dump()
0
 
sasnaktivAuthor Commented:
Hi Ray,
Yes I know fgetcsv() returns an array. But I'm looking to get the value of a particular entry (the first one) in a particular column. Whether it be $data[0] or $data[1] or $data[2] does not matter at this point.

For example, let's say $data[0] contains: Ray , Sas , Jack , Jill , etc.
I want to know if the first entry is "Ray" or not. I don't care about the subsequent values in the array.

I hope I'm being clear,
Sas
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Ray PaseurCommented:
Not sure I follow.  Are you saying that the first row of the CSV file contains this:
Ray,Sas,Jack,Jill

Open in new window

And you want to read only the first row of the CSV file, then isolate Ray?
0
 
sasnaktivAuthor Commented:
No. I only want "Ray" from $data[0] . Not "Sas, Jack, Jill"
So yes, I want to isolate "Ray".
Because once I have "Ray" I'll know how to use "Sas, Jack, Jill"
0
 
Ray PaseurCommented:
OK, the descriptions are still not making any sense.  Let's try it this way.

1. Make a small CSV file that is representative of your data.  A few lines is enough.
2. Make a small results set that shows exactly what you want to get from the CSV file.
3. Post both of those things here.  This is called the SSCCE and once it's here and clear, it should be easy to help you.
0
 
sasnaktivAuthor Commented:
Thanks Ray,
I'll try to explain this more clearly tomorrow.
It's a bit more complex than just creating the files you're requesting.
Good night!
Sas
0
 
sasnaktivAuthor Commented:
Hi Ray,
To prepare my files as SSCCE will only make this more complex than it needs to be. After all, it's simply a matter of logic. But importantly, being able to communicate that logic clearly without ambiguity (which it seems I'm not being very successful with at this point).

So hang in there with me.
This next explanation is the best way I could think of to spell out exactly what it is that I'm trying to accomplish. And that is to INSERT into my MySQL DB the values that are presented to me in differently configured CSV source files.

Therefore by being able to determine the value of the first entry in $data[0] of each CSV source file, I will learn the column configuration of the CSV files which will populate my MySQL DB.

With that knowledge, I can alter the MySQL INSERT to accommodate those differences between the CSV file structures so that the values are entered into my MySQL DB in the proper columns.

I've made 2 extremely simple CSV files (CSV_Example1.csv and CSV_Example2.csv) to demonstrate the kind of files I expect to be working with, and notes to outline my goals.
....................................................................................................
Begin "CSV_Example1.csv"
"E-mail Address","First Name","Middle Name","Last Name"
"sas@sas.com","Sas","Naktiv"
"ray@experts.com","Ray","Savant","Passeur"
End "CSV_Example1.csv"
....................................................................................................
Begin "CSV_Example2.csv"
Name,Given_Name,Middle_Name,Family_Name,eMail
Sas Naktiv,Sas,,Naktiv,sas@sas.com
Ray Savant Paseur,Ray,Savant,Paseur,ray@experts.com
End "CSV_Example2.csv"
....................................................................................................

Notice that in CSV_Example1.csv the first value of $data[0] is "E-mail Address".
And in CSV_Example2.csv the first value of $data[0] is "Name".
The difference in the $date[0] values between the two CSV files tells me that the CSV source files have different column structures from each other.
Accordingly, by knowing what the value of the first entry in $data[0] is in each CSV source file (I do not need to know the subsequent values ie.:sas@sas.com & ray@experts.com in Example1 -- nor -- Sas Naktiv & Ray Savant Paseur in Example2), I can configure the columns in the proper order to "INSERT" those fields into my MySQL DB which has yet a different column structure altogether.
As in:
if (the first value of $data[0]=='E-mail Address') {$InsertValuesIntoDB='InThisOrder';}
else if (the first value of $data[0]=='Name') {$InsertValuesIntoDB='InSomeOtherOrder';}

....................................................................................................

Ray, I hope I've been able to make my logic clear enough. Either that or I should move to Colorado where the air is clear and the smoke is dear.
Thanks in advance,
Sas
0
 
sasnaktivAuthor Commented:
Thanks Ray,
That seems to be working for me, but ...

The var_dump is giving me string(14) "E-mail Address"
I only want  E-mail Address

So, how can I avoid or delete the unwanted info  string(14) ...
as well as the quotes around " E-mail Address "  ?

Sas
0
 
Ray PaseurCommented:
The value you want is stored in the variable named $sig.  We use var_dump($sig) to prove that we got the expected value in the expected variable.

Whenever you're not sure what a PHP function is doing, or you're unsure of how to change the function call, you can look it up on PHP.net.

You can change var_dump(), which is used for diagnostic information and program debugging, into print() or echo.
0
 
sasnaktivAuthor Commented:
Thanks Ray.
As usual your help is much appreciated.
Sas
0
 
Ray PaseurCommented:
Glad to help -- thanks for using EE, ~Ray

ps: The Colorado idea sounds pretty good, too!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.