Important alert: (current site time 7/15/2013 10:10:45 AM EDT)
 

VB icon

A way to save arrays recursive into a file

Email
Submitted on: 3/19/2003 10:28:10 AM
By: FirebirdDE  
Level: Intermediate
User Rating: By 2 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 21771
author picture
(About the author)
 
     This little code snipplet can save and reload arrays recursive into/from a file. You can use this if you want to add a guestbook to your page and there's no MySql database left, for example.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A way to save arrays recursive into a file
// Description:This little code snipplet can save and reload arrays recursive into/from a file. You can use this if you want to add a guestbook to your page and there's no MySql database left, for example.
// By: FirebirdDE
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=948&lngWId=8//for details.//**************************************

<?php
	/* 
	
		Save arrays recursive into a file
		
		Construct of a file:
		\0{ keyname\0value\0keyname\0value\0keyname\0\0{SUBARRAY\0}\0}
		
		Syntax:
		
		SaveArrayToFile(Handle of a BINARY opened file, an array);
		LoadArrayFromFile(Handle of a BINARY opened file);
		
		NOTE: Its necessary to open the file
		 in binary mode if you're using M$ Windows.
		 Use "rb" and "wb" to open a file in binary mode.
		
		
		 Script by Firebird, www.berndt-cpk.de
		 Sorry for possible grammar faults, I'm no native speaker :)
		 
	*/
	
	
	// Save the array
	// Syntax: SaveArrayToFile(Handle of a BINARY opened file, an array);
	function SaveArrayToFile($vFile, $vArray)
	{
		// Every array starts with chr(1)+"{"
		fwrite($vFile, "\0{");
		
		// Go through the given array
		reset($vArray);
		// Start a loop. One could put the "next" command here, but if an 
		// entry was empty, "next" would return false.
		while (true) 
		{
			// Get the current "record" of the array
			$Current = current($vArray);
			// Get the current key. I use addshashes cause the key could
			// contain \0, and this would be interprated as End-Of-Record
			$MyKey = addslashes(strval(key($vArray)));
			
			// Is it a sub-array?
			if (is_array($Current)) {
				// Save the key into the file
				
				fwrite($vFile, $MyKey."\0");
				// Call me (the function) using the sub array
				SaveArrayToFile($vFile,$Current);
				// Write the record delemitter.
				fwrite($vFile, "\0");
			} else {
				// Save the record into the file			
				$Current = addslashes($Current);
				fwrite($vFile,"$MyKey\0$Current\0");
			}
			
			// Proceed to the next record. Skip Empty records
			++$i;
			while (!next($vArray))
			{
				if (++$i > count($vArray)) break;
			}
			if ($i > count($vArray)) break;
		}
		
		// Close current array
		fwrite($vFile,"\0}");
	}
	
	// Load an array
	// Syntax: LoadArrayFromFile(Handle of a BINARY opened file);
	function LoadArrayFromFile($vFile)
	{
		// Create empty array
		$ForRet = array();
		
		// Does the file contain an array?
		$Wert = fread($vFile,2);
		if ($Wert != "\0{") return;
		
		// Again, start a loop
		while (true) {		
				
			// Does the array end here?
			if (NextMatches($vFile,"\0}")) {
				// Read in the closer-string, otherwise the function would fail.
				fread($vFile,2);
				// Return the array
				return $ForRet;
			}
			
			// Get the key name
			$MyKey = "";
			while (true) {
				$Zeichen = fread($vFile,1);
				if ($Zeichen == "\0")
					break;
				else 
					$MyKey .= $Zeichen;
			}
			// Remove slashes
			$MyKey = stripslashes($MyKey);
			
			// Is it a sub-array ?
			if (NextMatches($vFile,"\0{")) {
				// It is a subarray ^^
				$ForRet[$MyKey] = LoadArrayFromFile($vFile);
				// Skip the delemitter
				fread($vFile,1);
			} else {
				// Read the value
				$MyVal = "";
				while (true) {
					$Zeichen = fread($vFile,1);
					if ($Zeichen == "\0")
						break;
					else 
						$MyVal .= $Zeichen;
				}
				// Parse the value into the array
				$MyVal = stripslashes($MyVal);
				$ForRet[$MyKey] = $MyVal;
			}
		
		// Continue
		}
	}
	
	// Check if $Text is @ cursor position in $vFile
	// Syntax: NextMatches($vFile, $Text);
	function NextMatches($vFile, $Text)
	{
		// Save the current position in the file
		$PrevPos = ftell($vFile);
		
		// How long is $Text ?
		$Jump = strlen($Text);
		
		// Check if the file is long enaugh
		$stats = fstat($vFile);
		if (ftell($vFile) + $Jump > $stats[7])
			return false;
		
		// Read out a string as long as $Text
		$Erg = fread($vFile,$Jump);
		
		// Continue to the previous position.
		// I don't use whence for compatibility with PHP < 4
		fseek($vFile, $PrevPos);
		
		return ($Erg == $Text);
	}
	
	/*
	
	An example:
	
	Create an recursive array and save it. Reload it again and give it out.
	
	----------------
	*/
	
	$MyGuestbook = array();
	$MyGuestbook[1]['Name'] = "Test 1";
	$MyGuestbook[1]['Test'][1][1] = "Test 1.2.1";
	$MyGuestbook[1]['Test'][2] = "Test 1.3";
	$MyGuestbook[2]['Name'] = "Test 4";
	$MyGuestbook[3]['Test'] = "Test 5";
	$MyGuestbook[4] = "\0";	
	
	for ($i=0; $i<15; $i++)
		$MyGuestbook['Subarray']['Verybig'][$i] = strval($i);
		
	$Datei = fopen("Test.txt","wb");
	SaveArrayToFile($Datei, $MyGuestbook);
	fclose($Datei);
	
	$Datei = fopen("Test.txt","rb");
	$My = LoadArrayFromFile($Datei);
		
	
	
	echo($My[1]['Name']."<br>");
	echo($My[1]['Test'][1][1]."<br>");
	echo($My[1]['Test'][2]."<br>");
	echo($My[2]['Name']."<br>");
	echo($My[3]['Test']."<br>");
	echo("6:: ".$My['Subarray']['Verybig'][6]."<br>");
	echo($MyGuestbook[4]=="\0" ? "The last value is chr(0)" : "Sorry, my fault");
	
	fclose($Datei);
	unlink("Test.txt");
	
	/*
	---------------------
	
	*/
?>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/20/2003 8:55:05 PMChristian Mallette

Not that your way is bad, but it would be easier if you just use:

Serialize($array)
Unserialize($array)

That's what those functions were made for.
(If this comment was disrespectful, please report it.)

 
3/21/2003 8:01:22 AMFirebirdDE

Didn't know those. Well, then lets say I just wanted to show how they're working :) Thx !!!
(If this comment was disrespectful, please report it.)

 
3/30/2003 3:51:27 PMChristian Mallette

Your way is cool and works, but it probably takes more memory to do than simply using Serialize(). I hope my comment helped you out :).
(If this comment was disrespectful, please report it.)

 
8/20/2004 3:22:07 PM

Problem is serialize
(If this comment was disrespectful, please report it.)

 
4/10/2006 7:26:38 AMmok

jhytty
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.