Solved

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

Posted on 2013-02-06
3
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
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 clear a vector as well as how to detect empty vectors in C++.
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 …

751 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