5

Is there a way to make the whole array content and make it a string so that i can save it. the string i want to save is same with the output of print_r( $Array ) function.

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

I'dd like to have that kind of string saved on a different file( a txt or php file) which will be made by the program that I'm doing.

5 Answers 5

15
$str = var_export($array, true);
Sign up to request clarification or add additional context in comments.

Comments

4

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

$str = print_r($arr, true);

Comments

4

I would use json_encode. That 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}

Comments

4

Well you can use serialise() function to convert an array into string.

e.g. 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.

1 Comment

The same way you can implement serialize() function for multidimensional array
0

Your Database Table Structure Collation (php_my_admin) should also be defined accordingly as the default Latin latin1_swedish_ci didn't work for me. I used utf8_bin

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.