Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-02-06
3
Medium Priority
?
470 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

704 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