Solved

Php parsing csv text file with line ending "\n" to multidemensional array

Posted on 2013-02-06
3
451 Views
Last Modified: 2013-02-06
I write to file  this way:
$var = "$date, $firstname,$lastname, $address, $city, $state, $zip, $product,$price\n";

File looks like below after write without star break

*************************************
date, firstname, lastname, address, city, state, zip, product, price
date, firstname, lastname, address, city, state, zip, product, price
date, firstname, lastname, address, city, state, zip, product, price
date, firstname, lastname, address, city, state, zip, product, price
*************************************

I need load each line into a $multidemensional[ ] [ ] array so that i can do this
Get lastname  by $multidemensional[$i][2] to sort by last name.

I have tried different functions without success to write out multidemensional array...i dont get an error...just no output.
here is my code to test
echo $multidemensional[1][2]."<br/>"; which does not output second row lastname.

i need the array load to multidemension function  and most efficient way to sort it..by last name
thanks

I am using php 5.3 and above
0
Comment
Question by:10023
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
ID: 38862574
you could do something like this to read the file into an array and sort it.


<?php
$array = array();
$file_handle = fopen("test1.txt", "r");
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   $line = array_map('trim', explode(',', $line)); // $line is now an array
   array_push($array, $line);
}
fclose($file_handle);
usort($array, 'sort_last_name');
print_r($array);

function sort_last_name($a, $b) {
    if ($a[2] > $b[2]) {
        return 1;
    } elseif ($a[2] < $b[2]) {
        return -1;
    } else {
        return 0;
    }
 
}

?>

Open in new window


That will read the contents from a file into an array, then it will sort it by the 3rd index in that array (which is 2 or the last name).
0
 
LVL 10

Author Comment

by:10023
ID: 38862614
Thankyou trying it out now...appears to be working..need to output sorted without using
print_r so output would be cleaner...trying for loop
0
 
LVL 10

Author Closing Comment

by:10023
ID: 38862698
thankyou very much...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

12 Experts available now in Live!

Get 1:1 Help Now