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

user3900720
  • 131
  • 2
  • 8
  • Make an attempt here a link for a start http://php.net/manual/ro/function.file-get-contents.php – Mihai Aug 03 '14 at 09:50

3 Answers3

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

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39
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

Community
  • 1
  • 1
user3781087
  • 409
  • 4
  • 4
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!

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
  • Why are you still using old PHP4 functions? – Charlotte Dunois Aug 03 '14 at 10:02
  • What is the logic behind creating a class and create for each line a new class object? – Charlotte Dunois Aug 03 '14 at 10:06
  • @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. – christopher Aug 03 '14 at 10:23
  • 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 – Charlotte Dunois Aug 03 '14 at 10:25
  • 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 – christopher Aug 03 '14 at 10:27