alex
alex

Reputation: 79

array_reverse() expects parameter 1 to be array, string given in

I am grabbing a .txt file and trying to reverse it, but I get this error when I try to, I don't understand it. Help please?

array_reverse() expects parameter 1 to be array, string given in ......

Here is the code:

$dirCont = file_get_contents($dir, NULL, NULL, $sPoint, 10240000);
$invertedLines = array_reverse($dirCont);

echo $invertedLines;

Upvotes: 0

Views: 8199

Answers (2)

Nirmal Khadka
Nirmal Khadka

Reputation: 21

I think you need to pass the value on an array.

array_reverse(array($dircont));

This is working fine for me.

Upvotes: 1

Suroot
Suroot

Reputation: 4423

A string is not an array? Even if it were (as in C strings) it would not work as you expected. You'll need to split the file on line breaks (if you're trying to reverse to get the end of the file first).

$invertedLines = array_reverse(preg_split("/\n/", $dirCont));

Upvotes: 2

Related Questions