Answers for "php load file into array"

PHP
7

php save array to file

$arr1 = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
file_put_contents("array.json",json_encode($arr1));
# array.json => {"a":1,"b":2,"c":3,"d":4,"e":5}
$arr2 = json_decode(file_get_contents('array.json'), true);
$arr1 === $arr2 # => true
Posted by: Guest on March-09-2020
0

php read text file into array

//php read a text file into an array
//each item in the array is a line in the text file
//file.txt looks like this:
//hello
//testing
//text
//end file.txt (the file doesn't include the // marks
$file = file("http://example.com/file/file.txt");
echo $file[0]; //prints hello
echo $file[1]; //prints testing
echo $file[2]; //prints text
Posted by: Guest on August-02-2021

Browse Popular Code Answers by Language