2

I have a file of semi-colon separated values, say something like this:

1;data
2;data
3;data
...
Z;data

The number of lines Z can change per file. The idea is to parse the data and put it into a database. So I do the following:

$contents = file_get_contents($filename);
$line = explode(chr(13),$contents);

And now I go through line by line as follows and things are ok:

$n = 0;
while ($line[$n] != "") {
    //lots of magic stuff that works
    $n = $n + 1;
}

Now for reasons not necessary to explain, I need to parse line Z first and then go down to 1 instead. How should I simply go about doing this?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Geoff
  • 925
  • 4
  • 14
  • 36
  • 1
    Just reverse the array before looping through it – John Conde Jun 03 '15 at 19:31
  • You say it's not necessary to explain, but I'll ask anyway: why do you *need* to read the lines in reverse? – salathe Jun 03 '15 at 19:43
  • @salathe the file is created automatically and in a time order. Sometimes, later inputs can correct earlier inputs. The db key won't allow both entries to be included, and since I want the correct version, this solves the problem. I hope this makes sense. – Geoff Jun 03 '15 at 19:47
  • @rizier123 I think the title should include the 'explode' since really that was where my problem was: explode creates an array and so reversing the array becomes obvious (well, obvious now, thanks to the answers below!) – Geoff Jun 03 '15 at 19:50
  • Thanks @Geoff, it does make sense. However, it still seems like an odd approach to the problem. Why not "correct" earlier inputs with later ones, in the db? In pseudo-code: "if the key exists, update with new values, otherwise insert new row". (Just an idea, looping the file in reverse is cool too.) – salathe Jun 03 '15 at 19:51
  • @Geoff also as a random aside, do you know about the [`file()`](http://php.net/file) function? It can replace your `file_get_contents()` and `explode()` lines, to give you an array in a single, simple call. – salathe Jun 03 '15 at 19:52
  • @salathe Although the file is automatically created, I only parse it once it is completed. Surely input data and then updating is more hassle. Will look into `file()`, many thanks! – Geoff Jun 03 '15 at 19:56
  • @Geoff cool, cool, I'm not going to push you. It's all "hassle" in the end, be it looping over the file backwards, or forwards, or updating db rows. :) – salathe Jun 03 '15 at 20:04

2 Answers2

6

You can do this with 2 ways:

1. Simply reverse your array, e.g.

$array = array_reverse($array);

And then you can loop through as followed:

foreach($array as $v) {
    //Your magic stuff with $v
}

2. Count the elements of the array

First count the elements of the array and then loop through until index 0.

$count = count($array)-1;

while($count >= 0) {
    //Your magic stuff with $array[$count]
    $count--;
}

I would choose the first way, since you have a lot less variables and it's much cleaner this way.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

If I understand you correctly, you want to, during the while loop, go from the last cell in the array to the first. So to do that you can go:

$n = (count($line) - 1);

while ($line[$n]!="") {
 lots of magic stuff that works
$n=$n-1;
}
A3mercury
  • 166
  • 1
  • 7