0

Hello I have a text file with three lines. every line has a float number. How can I take each value and put in a float variable even if array?

For example text file is like the following

+3.01\n
-0.0012\n
-0.1\n

I want an array [if it possible] which save the values as float not as string.

Thanks

1
  • what prevents you from reading values into array and then loop over it and cast values to floats? Commented Aug 1, 2010 at 10:53

1 Answer 1

5
$fileArr = file($fileName);
$fileArr = array_map('floatval', $fileArr);
Sign up to request clarification or add additional context in comments.

1 Comment

could make it a one-liner $fileArr = array_map('floatval', file($fileName));

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.