Problem sorting arrays from tab delimited file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scrypte
Forum Newbie
Posts: 6
Joined: Sat Nov 06, 2004 3:31 pm

Problem sorting arrays from tab delimited file

Post by scrypte »

ok i'm starting over again with my coding.

Code: Select all

<?  

$readfile = file("../../voodoo/rooms.txt");

echo "<pre>";
print_r($readfile);
echo "</pre>";

?>
I get this:

Code: Select all

Array
(
    [0] => GATE	The Gate	voodoo://chat.fearlab.net=GATE	0	250	0	GEN

    [1] => POTTER	harry potter net	voodoo://chat.fearlab.net=POTTER	0	30	0	GEN

    [2] => SYCOSI	Syc0sis	voodoo://chat.fearlab.net=SYCOSI	1	30	0	GEN

    [3] => HANG	H a N g O uT	voodoo://chat.fearlab.net=HANG	0	30	0	GEN

    [4] => GUYZ	Syc0 Guyz	voodoo://chat.fearlab.net=GUYZ	0	30	0	MEN

    [5] => SOUTH	Southern Comfort Music Room	voodoo://chat.fearlab.net=SOUTH	0	30	0	MUS

    [6] => CROSS	The Crossing	voodoo://chat.fearlab.net=CROSS	0	30	0	GEN

    [7] => SHELLY	_shelley_'s Room	voodoo://chat.fearlab.net=SHELLY	0	30	2	GEN

    [8] => IDOL	syc0idol	voodoo://chat.fearlab.net=IDOL	0	30	0	GEN

    [9] => AUCTIO	Syc0sis Auction	voodoo://chat.fearlab.net=AUCTIO	0	60	0	GEN
)
Now I need to go down one more level, how can I do that?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Problem sorting arrays from tab delimited file

Post by volka »

you can either use explode() or fgetcsv() for this task
scrypte
Forum Newbie
Posts: 6
Joined: Sat Nov 06, 2004 3:31 pm

Post by scrypte »

Ok I am still having trouble grouping the entire array properly, its still splitting it up by row:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => GATE
            [1] => The Gate
            [2] => voodoo://chat.fearlab.net=GATE
            [3] => 0
            [4] => 250
            [5] => 0
            [6] => GEN

        )

)

Array
(
    [1] => Array
        (
            [0] => POTTER
            [1] => harry potter net
            [2] => voodoo://chat.fearlab.net=POTTER
            [3] => 0
            [4] => 30
            [5] => 0
            [6] => GEN

        )

)
I need it to do this:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => GATE
            [1] => The Gate
            [2] => voodoo://chat.fearlab.net=GATE
            [3] => 0
            [4] => 250
            [5] => 0
            [6] => GEN

        )
    [1] => Array
        (
            [0] => POTTER
            [1] => harry potter net
            [2] => voodoo://chat.fearlab.net=POTTER
            [3] => 0
            [4] => 30
            [5] => 0
            [6] => GEN

        )

)
here is the new coding:

Code: Select all

<?  

$readfile = file("../../voodoo/rooms.txt");

foreach ($readfile as $k => $v ) {	
	
$readfile = array($readfile[$v][$k] = split("\t", $v));
    
	echo "<pre>";
	print_r($readfile);
	echo "</pre>";
	
} 

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

lose the leading "$readfile ="

And don't use split(), but explode().
scrypte
Forum Newbie
Posts: 6
Joined: Sat Nov 06, 2004 3:31 pm

Post by scrypte »

sorry that doesnt work either and what do you mean loose the first $readfile
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It's probably easier to use fgetcsv().

Code: Select all

<?php
$sourcefn = 'data.txt';
$fd = fopen($sourcefn, 'r') or die('cannot open file: ' . $sourcefn);

$readfile = array();
while(!feof($fd)) {
	$readfile[] = fgetcsv($fd, 0, "\t"); // php 5.0.4 or higher
}

print_r($readfile);
?>
Post Reply