how to convert column data in text or csv file into array in php


The data present in a text file or csv file.so we have total of six column.

19738424064    +19096060558    2012-05-01 09:04:17    2012-05-01 09:05:44    120    0.008
19738424064    +19096060558    2012-05-01 12:01:52    2012-05-01 12:19:17    1080    0.072
19738424064    +19096060558    2012-05-01 14:27:30    2012-05-01 14:27:45    60    0.004
19738424064    +19096060558    2012-05-01 14:28:02    2012-05-01 14:28:12    60    0.004
19738424065    +19096060558    2012-05-01 14:14:17    2012-05-01 14:18:49    300    0.02
19738424065    +19096060558    2012-05-01 14:28:24    2012-05-01 14:29:58    120    0.008
19738424064    +19096060558    2012-05-02 09:20:19    2012-05-02 09:21:19    60    0.004
19738424064    +19096060558    2012-05-02 09:08:04    2012-05-02 09:19:46    720    0.048
19738424065    +19096060558    2012-05-02 09:21:33    2012-05-02 09:39:27    1080    0.072
19738424064    +19096060558    2012-05-02 14:16:54    2012-05-02 14:23:49    420    0.028

The code to convert each column to array with name of header1,header2,header3,header4,header5,header6.

$mainarray=array();//deafault array
foreach (file('/12-2013.txt') as $row)
{
//var_dump($row);
//var_dump(array_unique($row));
//$mainarray[] = array_unique($mainarray);
$list = list($header1, $header2, $header3, $header4, $header5, $header6) = explode("    ",$row);
//var_dump($list);
$mainarray[] = array('header1'=> $header1, 'header2' => $header2,'header3' => $header3,'header4' => $header4, 'header5' => $header5, 'header6' =>$header6);
}

var_dump($mainarray);

By passing the key to array we are able to get particular column from the text or csv file.

what is the use of sharing this code?

You may use code in creating google api graph or any to have control data in the text or csv file by using php.

Advertisements
About

myself pramodh kumar yet another php developer from India and have worked on oops,procedural,yii framework,codeigniter,wordpress,joomla,api and more.

Tagged with: , , , , , , , , ,
Posted in google chart, php

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow php tutorial on WordPress.com
categories
Calendar
January 2014
M T W T F S S
« Dec   Feb »
 12345
6789101112
13141516171819
20212223242526
2728293031  
Follow me on Twitter
Blog Stats
  • 75,930 hits
%d bloggers like this: