Solved

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

Posted on 2014-03-29
12
333 Views
Last Modified: 2014-03-30
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

0
Comment
Question by:sasnaktiv
  • 6
  • 6
12 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39963956
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 39963972
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39964111
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:sasnaktiv
ID: 39964153
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39964332
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 39964359
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 39964519
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39964783
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
 
LVL 1

Author Comment

by:sasnaktiv
ID: 39964872
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39965047
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
 
LVL 1

Author Closing Comment

by:sasnaktiv
ID: 39965072
Thanks Ray.
As usual your help is much appreciated.
Sas
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39965078
Glad to help -- thanks for using EE, ~Ray

ps: The Colorado idea sounds pretty good, too!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question