Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A way of storing an array on disk

Status
Not open for further replies.

RbgoWeb

Programmer
Apr 10, 2002
91
0
0
NL
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:
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');
 
It's not a very good idea to submit code like this as this thread will, due to posting inactivity, sooner or later disappear off the end of the thread list. If you think the code is useful to someone, create a FAQ.

Also, it would seem to me that if you used the PHP builtin function var_export() you could really simplify your code.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Don't feel it is necessary to make a FAQ for this. I knew about var_export() but I just like doing some things myself.
var_export() is more advanced but I don't like the unneeded array keys it sets for a zero based array.
 
Out of curiosity, if you don't care whether your code is available in the long-term to Tek-Tips members, why did you post it?



Want the best answers? Ask the best questions! TANSTAAFL!
 
I do care about it. That's why I posted it, sleipnir.
At least it is code what is posted...
It seems me that you know your way around here much better than me, and are much more organized. Why don't you make sure this piece of code ends up in exactly the right place if you really care this code is available in the long-term to Tek-Tips members.
 
Why don't you make sure this piece of code ends up in exactly the right place if you really care this code is available in the long-term to Tek-Tips members.
Because it is your code, not mine. If you look in the FAQ section of this forum, you'll see several examples of times where I have posted my own code to FAQs. But I do not post other people's code to my FAQ.

Unless someone continues to post to this thread, it will in less than a week drop off the first page of the thread list for this forum. Once that happens, it is very unlikely that anyone will ever again see or ever be helped by your code.



BTW, instead of creating a PHP-code version of your array to store in the file, why not just use serialize() and unserialize()?

It's your code and you can, of course, do what you want with it. I just don't understand why, if you're going to post this out-of



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thank you for making it more clear to me, and for the FAQ. I don't visit this forum that much so I'm not that familiar with all the features yet. The other day I saw a thread about someone looking for a way to store variables on disk and I have this function that does it, so I made a tip page for it, thinking/soundslike that's what tips are for.

I'm not using serialize() functions, for readability of the code files, and also assuming that ready to include php code files load faster that unserialize. But I might assume wrong. Do you know what is faster?
 
Your code is interpreted. unserialize() uses compiled code.

I don't know for certain, but I'd be suprised if unserialize() wasn't significantly faster.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top