Re: note 15149 added to ref.array
| From: | Jeroen van Wolffelaar | Date: | Wed, 29 Aug 2001 17:52:19 +0000 |
| Subject: | Re: note 15149 added to ref.array | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-17333@lists.php.net to get a copy of this message | ||
Use serialize/deserialize: more efficient, and easier.
Make sure to escape it by means of addcslashes($input,'\'\\'), before you
put it inside single quotes
--jeroen
----- Original Message -----
From: <wcook@nuvox.net>
Newsgroups: php.notes
To: <php-notes@lists.php.net>
Sent: Wednesday, August 29, 2001 7:45 PM
Subject: note 15149 added to ref.array
> array_to_code
>
> I needed to cache a large array so I wrote a function to turn an array
into a string that can be written to a file. The file containing the output
of array_to_code can be included by a php script therefore creating the
array in the script. This seems to be much faster than
serializing/unserializing a large array structure to and from a file or a
database. This function will only handle arrays containing strings,
integers, or other arrays.
>
> I use the code to cache the tree structure for a web site. The data for
the tree structure is stored in a database and can be changed without
changing the web site. When I want to post changes to the site structure I
just re-create the site structure file using the array_to_code function.
The array in the site structure file can be traversed without hitting the
database at every branch of the tree.
>
> <?
> // turn an array into php code
> //
> function array_to_code(&$array, &$out, $indent = 0) {
> $indent += 5;
> if (!is_array($array)) {
> return;
> }
> if (sizeof($array)) {
> $out .= 'Array ('."\n";
> $out .= indent_sp($indent);
> $first = 1;
> foreach ($array as $key => $val) {
> if ($first)
> $first = 0;
> else
> $out .= ",\n".indent_sp($indent);
> if (is_array($val)) {
> $out .= '"'.$key.'" => ';
> // recurse
> array_to_code($val, $out, $indent);
> } else {
> if (is_numeric($val))
> $out .= '"'.$key.'" =>
> '.ereg_replace("\"", "\\\"",
$val);
> else
> $out .= '"'.$key.'" =>
> "'.ereg_replace("\"", "\\\"",
$val).'"';
> }
> }
> $out .= "\n".indent_sp($indent - 5).' ) ';
> return $out.';'."\n";
> }
> }
>
> function indent_sp($num) {
> for ($i = 0; $i < $num; $i++) {
> $out .= " ";
> }
> return $out;
> }
>
> // test Array
> $test_array = Array("one" => "test1",
> "two" => "test2",
> "three" => "test3",
> "four" => Array ("four_sub_1" =>
> "test4_1",
> "four_sub_2" => "test4_2",
> "four_sub_3" => "test4_3",
> "four_sub_4" => "test4_4",
> "four_sub_5" => Array
("third_level_1" => "test_1",
>
"third_level_2" => "test_2",
>
"third_level_3" => "test_3"),
> "four_sub_6" => "test4_6",
> "four_sub_7" => "test4_7"
> ),
> "five" => "test5",
> "six" => 6,
> "seven" => "test7",
> "eight" => 'This is a quote test " " "
> ');
>
>
> // output php code
> $out = '';
> array_to_code($test_array, $out);
> print "<pre>";
> print $out;
> print "</pre>";
> ?>
>
> http://www.php.net/manual/en/ref.array.php
>