Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

615 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