Solved

PHP Parse CSC

Posted on 2009-04-14
14
363 Views
Last Modified: 2013-12-12
I am trying to upload a .csv file and then parse it out.. I have the file uploading with no problem and can read the file using fgetcsv just fine.. However, I am stuck on how to parse the arrays. I have tried multiple options but I guess just do not understand what I am trying to do.. I have posted some of the array below but I would like to do is get each set of data separated so I can loop through them individually.. any help is greatly appreciated.
Array ( [0] => TELEPHONE_NUMBER [1] => BTN [2] => OCN [3] => DCR [4] => PDCR [5] => COMPLETION_DATE [6] => LN [7] => ORDER_NUMBER [8] => APPLICATION_DATE [9] => PON [10] => CIRCUIT_ID [11] => CLASS_SERVICE ) Array ( [0] => 999-999-9999 [1] => 0 [2] => 1111 [3] => AS [4] => [5] => Apr 9, 2009 [6] => (NON-PUB) SMITH, JOHN [7] => D1DJD7J6 [8] => Apr 6, 2009 [9] => [10] => [11] => 1F1CL ) Array ( [0] => 999-999-9999 [1] => 0 [2] => 1111 [3] => AS [4] => [5] => Apr 9, 2009 [6] => DOE, JOHN [7] => D1FC3291 [8] => Apr 9, 2009 [9] => 2.09E+13 [10] => [11] => 1F3CL ) Array ( [0] => 9999-999-9999 [1] => 0 [2] => 1111 [3] => AS [4] => [5] => Apr 9, 2009 [6] => JONES, JOE [7] => D60W11Y2 [8] => Apr 8, 2009 [9] => 0408JOO [10] => [11] => 1FR )

Open in new window

0
Comment
Question by:vertex_paul
  • 8
  • 6
14 Comments
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24139815
If you parse your array like this, you can access your recorords like this:
var_dump($arrData[0]);

for the first record for example.
the first array is numeric so you have to access it like $arrData[0]
the array in it now is associative, so you can use the keys. $arrData[0]['TELEPHONE_NUMBER']
there are a bunch of array functions explained on the php site, take a look at this: http://be.php.net/manual/en/function.array.php
$firstline = true;

$arrData = array();

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

   if($firstline){
 

     $keys = $data;

     $firstline= false;

     continue;
 

  }// end if
 

  $arrData =  array_combine($keys,$data);
 
 

}// end while
 

echo "<pre>";

var_dump($arrData[0]['TELEPHONE_NUMBER']); // string ("999-999-9999")

echo "</pre>";

Open in new window

0
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24139840
oops one little error i see...

change
$arrData =  array_combine($keys,$data);

into
$arrData[] =  array_combine($keys,$data);

sorry
0
 
LVL 2

Author Comment

by:vertex_paul
ID: 24139876
Thanks for the quick response.. I understand what you are saying.. how now would I look through each array and pull out that ones information and go to the next one?
0
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24139946
You can do it with a foreach loop like this.
While foreaching you can offcourse do all kinds of logic with it or echo a table in between.
echo "<pre>";

foreach($arrData as $data){
 

    var_dump($data);
 

}// end foreach

echo "</pre>";

Open in new window

0
 
LVL 2

Author Comment

by:vertex_paul
ID: 24140038
ok.. maybe I am not following you as well I as I thought.. below is my code
$file = fopen($target_path,"r");

while(! feof($file))

  {

  //print_r(fgetcsv($file));

  $data [] = fgetcsv($file);

  }

  

$firstline = true;

$arrData = array();

while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {

 

   if($firstline){

 

     $keys = $data;

     $firstline= false;

     continue;

 

  }// end if

 

  $arrData =  array_combine($keys,$data);

  
 

    echo "<pre>";

foreach($arrData as $data1){

 

    var_dump($data1);

 

}// end foreach

echo "</pre>";

 

}// end while

 

 

  

fclose($file);

Open in new window

0
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24140040
More info on foreach and looping arrays/data:
http://php.net/foreach
0
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24140066
Hehe noooo ;)
put the foreach outside the while.
In the while loop we build up the array, then afterwards you can parse it easely in a foreach.
$file = fopen($target_path,"r");

while(! feof($file))

  {

  //print_r(fgetcsv($file));

  $data = fgetcsv($file);

 

   if($firstline){

 

     $keys = $data;

     $firstline= false;

     continue;

 

  }// end if

 

  $arrData[] =  array_combine($keys,$data);

  

 

}// end while

  

fclose($file);
 
 

echo "<pre>";

foreach($arrData as $data1){

 

    var_dump($data1);

 

}// end foreach

echo "</pre>";

 

}// end while

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:phpmonkey
ID: 24140080
Ignore my last comment, it was some copy paste mess-up from the textbox.
This should be better:
$file = fopen($target_path,"r");

while(! feof($file))

  {

  $data = fgetcsv($file);

 

   if($firstline){

 

     $keys = $data;

     $firstline= false;

     continue;

 

  }// end if

 

  $arrData[] =  array_combine($keys,$data);

  

 

}// end while
 

fclose($file);
 

echo "<pre>";

foreach($arrData as $data1){

 

    var_dump($data1);

 

}// end foreach

echo "</pre>";

Open in new window

0
 
LVL 2

Author Comment

by:vertex_paul
ID: 24140124
Error:

Warning: array_combine() expects parameter 1 to be array, null given
0
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24140141
put

$firstline = false;

on top of the script.
$firstline = false;

$file = fopen($target_path,"r");

while(! feof($file))

//...

Open in new window

0
 
LVL 4

Accepted Solution

by:
phpmonkey earned 500 total points
ID: 24140146
dammid,

$firstline = true;

Sorry, its with beeing fast...
0
 
LVL 2

Author Comment

by:vertex_paul
ID: 24140172
Still same error.. pardon my ignorance but if the firstline if is not met then would $keys value not have to be set somewhere?
0
 
LVL 2

Author Comment

by:vertex_paul
ID: 24140213
That worked!!! .. Thank you very much.. shouldnt have been that hard but I was stuck
0
 
LVL 2

Author Closing Comment

by:vertex_paul
ID: 31569977
That worked!!! .. Thank you very much.. shouldnt have been that hard but I was stuck
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now