Save entire php array as string

Is there a way to make all the contents of the array and make it a string so that I can save it. the line I want to save matches the output of the print_r ($ Array) function.

Array ( [0982385099] => Array ( [Title] => The Key of Life; A Metaphysical Investigation [ISBN] => 0982385099 [Author] => Randolph J. Rogers [SalesRank] => 522631 ... 

I would like to have a line saved in another file (txt or php file) that will be made by the program I am doing.

+4
source share
4 answers
 $str = var_export($array, true); 
+10
source

Passing true as the second parameter to the print_r function will allow you to display the output of print_r .

 $str = print_r($arr, true); 
+3
source

I would use json_encode . This is because every browser can parse it .

code:

 <?php $ar = array( "1" => "Hello world!", "2" => 2 ); echo json_encode($ar); 

output:

 {"1":"Hello world!","2":2} 
+3
source

Well, you can use the serialise () function to convert an array to a string.

 eg we have an array $arr $arr = Array( "0" => "Dipendra", "1" => "Kshitiz", "2" => "Kushal", "3" => "Nirmal", "4" => "Prabin", "5" => "Prakash", "6" => "Sujit" ); echo serialise($arr); Now if we use serialise() function for this array we can view the following output a:7:{i:0;s:8:"Dipendra";i:1;s:7:"Kshitiz";i:2;s:6:"Kushal";i:3;s:6:"Nirmal";i:4;s:6:"Prabin";i:5;s:7:"Prakash";i:6;s:5:"Sujit";} 

Thus, we can use the array as a string.

+3
source

Source: https://habr.com/ru/post/1338685/


All Articles