0

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;

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

6 Comments

For clarification, what Suroot is trying to say is that the file_get_contents function you're using returns a string, not an array, so you're feeding array_reverse a string, which of course isn't going to work, us3.php.net/file_get_contents
Thank you Suroot, however I am recieving another error now, and the same array_reverse error aswell: Warning: preg_split() [function.preg-split]: Unknown modifier 'v' in
Can you paste the new code that you're using? Seems like you're trying to pass preg_split some flags that it's not happy with. Also, post all of the errors that you are seeing. Probably be best to edit the original post with the new information.
$dirCont = file_get_contents($dir, NULL, NULL, $sPoint, 10240000); $invertedLines = array_reverse(preg_split($dirCont, "\n")); Two errors are: Warning: preg_split() [function.preg-split]: Unknown modifier 'v' in and Warning: array_reverse() expects parameter 1 to be array, boolean given
Ha, my fault; not having a good day today; need to reverse the parameters in preg_split(). Will update, sorry for the confusion. Also need to include // around the pattern, hence /\n/
|
1

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

array_reverse(array($dircont));

This is working fine for me.

Comments

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.