This is a usefull function called 'arr_code', which makes a php code text representation of an array. The comments explain the ins and outs and how it works.
arr_code.php:
Here is how to use it...
arr_code.php:
Code:
[COLOR=#AAAAAA]/*
DESC:
Make a php code text representation of an array.
PARAM:
+ $code (OUT, STR, PBRS=REQ)
Receives the code text which can be stored inside a .php file like...
file_put_contents('myarray.php', ('<?php return '.$code.'; ?>'));
... and then get it loaded back in the program like this
$myarray = include('myarray.php');
+ $arr (IN, ARR, PBRS=REQ)
Can be any array, associative or zero based, nested as well
+ $bcat (IN, BOOL, PBRS=YES)
true: append to $code
false: assign to $code; start new text
+ $nl (IN, STR, PBRS=YES)
set new line character; by default all code text is on a single line
+ $z (IN, INT, PBRS=YES, PRIVATE)
counts the re-entry number; the Xth time this function calls itself.
RETURNS:
+ nothing
NOTES:
+ assert: types_in_array(integer, string, array)
+ assert: do_not_use_params($z)
+
*/[/color]
function arr_code(&$code, &$arr, $bcat, $nl='', $z=0)
{
[COLOR=#AAAAAA]//(1)[/color]
if(!$z){ if($bcat){ $code .= 'array('.$nl; }else{ $code = 'array('.$nl; } }
[COLOR=#AAAAAA]//(2)[/color]
$b_zba = true;
$key = array_keys($arr);
$len = count($key);
$i = 0;
while($i < $len){
if($key[$i] === $i){ $i++; continue; }
$b_zba = false;
break;
}
if($b_zba){ $len = count($arr); }
$i = 0;
while($i < $len){
[COLOR=#AAAAAA]//(3)[/color]
if($i){ $code .= ','.((!$z)?$nl:''); }
[COLOR=#AAAAAA]//(4)[/color]
if($b_zba){
$value =& $arr[$i];
[COLOR=#AAAAAA]//(5)[/color]
}else{
$k =& $key[$i];
$value =& $arr[$k];
if(is_integer($k)){
$code .= $k.'=>';
}else{
$code .= '\''.$k.'\'=>';
}
}
[COLOR=#AAAAAA]//(6)[/color]
if(is_array($value)){
$code .= 'array(';
arr_code($code, $value, $bcat, $nl, $z+1);
$code .= ')';
[COLOR=#AAAAAA]//(7)[/color]
}else if(is_numeric($value)){
$code .= $value;
[COLOR=#AAAAAA]//(8)[/color]
}else{
$code .= '\''.(str_replace("\\\"", '"', addslashes($value))).'\'';
}
$i++;
}
[COLOR=#AAAAAA]//(9)[/color]
if(!$z){ $code .= $nl.')'; }
}
[COLOR=#AAAAAA]/*
COMMENTS:
(1) first time called; open array text
(2) find out if array is zero based; tells wether we retrieve values, and write
code text with zero based index or array keys
(3) put every entry in the first array on a new line; within these entries all
text on same line
(4) array is zero based; use index to get value
(5) array is associative; use keys to get value; set element key in code text
(6) value is array; go for re-entry
(7) value is numeric; add as it is
(8) value is string; add between single quotes and make it save
(9) first time called; close array text
*/[/color]
?>
Here is how to use it...
Code:
[COLOR=#AAAAAA]//setup an array with test data[/color]
$arr = array(
array(1,'first',array(1,2,3,'this is not a tree')),
array(2,'next',array(4,'and',5,'storing to and from a disk drive')),
array(3,'hey!',array('this is'=>'an "associative" array', 6, 7, 8=>'10 years is a decade')),
);
[COLOR=#AAAAAA]//make php code text representation of the array[/color]
arr_code($code, $arr, false, "\n");
[COLOR=#AAAAAA]//store the code text in a file[/color]
file_put_contents('myarray.php', ('<?php return '.$code.'; ?>'));
[COLOR=#AAAAAA]//include the file[/color]
$myarray = include('myarray.php');