Solved

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

Posted on 2013-02-06
3
453 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

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

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 …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

943 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

6 Experts available now in Live!

Get 1:1 Help Now