1

I hava a file that contains :

ali 123456
vali 154667

I want to read this file and split into array like this:

$array[0][0]=ali 
$array[0][1]=123456
$array[1][0]=vali
$array[1][1]=154667 

I searched about this and saw w3schools documents, But I can't read my wanted! Please show me How should I do it!

Best Regard, Minallah Tofig

1

3 Answers 3

3

You can use the php function file() to read the file line by line into an array. Then you have to loop through it and explode() by the white space.

$array = file('file.txt.');
foreach($array as $key => $line) {
    $array[$key] = explode(" ", $line);
}

http://ch2.php.net/manual/en/function.file.php

http://ch2.php.net/manual/en/function.explode.php

Sign up to request clarification or add additional context in comments.

Comments

1

Firstly, initialize the array:

$myArray = array();

open the file:

$file = fopen("myfile.txt", "r");

iterate the file line by line

while (!feof($file)) {
   $line = fgets($file);
   $myArray[] = explode(' ', $line);
}

close the file

fclose($file);

$myArray contains your result

1 Comment

Thank You, Your Answer Is My Wanted!
0

Firstly, this question explains how you read a file line by line in PHP. The top and bottom of it is:

$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
    }
} else {
    // error opening the file.
}  
fclose($handle);

Now in your case, you've got a structured line. Namely.

<name><space><value>

I would strongly, strongly argue against using a multi dimensional array for this. Instead, go for an OOP based solution. First, define a class.

class Person {
    protected $name;
    protected $code;

    function __construct($line) {
        // Split and load.
    }
}

Then you can create an array of Person objects.

$people = array();

Then cycle through each line and create a Person object.

while(($line = fgets($handle)) !== false) {
    $people[] = new Person($line);
}

Now you've got all the pieces of the puzzle, it's up to you to put them all together!

5 Comments

Why are you still using old PHP4 functions?
What is the logic behind creating a class and create for each line a new class object?
@CharlotteDunois I just pulled from the other answer; didn't want to replicate answers. I'll upvote yours for using the later code. And the logic is, it's much, much easier to conceptualise. It's better to have a list of Person objects rather than an ugly, multi-dimensional array of values.
Well, you have to weigh the cost and use-factor of this class. The cost factor of an 'ugly, multi-dimensional array' (I think it's better than using a class for something you wouldn't need one) is much less than building a class. @christopher
But the next guy turning up will have a much easier time with $person->getName() then $array[$key][0][0]. It's not just about the efficiency of the code. It's about making maintainable code. Obviously it's just two different approaches to development. @CharlotteDunois

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.