Online Shopping : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 23 people (74%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Does PHP have a dictionary object like the one in ASP?
How do I do linked lists?
How do I make an associative array?

Mar 24th, 2009 18:42
chat alarab, games games, mike dillon, Matt Gregory,


There is no dictionary object in PHP because PHP is a pure language.  
Like C/C++, Java, and yes, even BASIC the language itself has no 
objects included with it.  Objects are language derivatives which are 
built off of the base structure of the language.  In Microsoft Visual 
C++, the language itself does not have a CString object.  This object 
is part of the MFC library.  (Microsoft Foundation Classes).  The 
library and IDE (Integrated Development Environment) are what 
Microsoft 
is *really* selling.  Their compiler is no different than Borland's or 
any of the free compilers available.
Now that doesn't mean that the functionality is denied you.  PHP 
arrays 
are designed to be very flexible.
	(Example 1:) $fruits = array
("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
	$fruits[a] = orange while $fruits[0] = "lemon";
	This is called an associative array (Effectively the same 
thing 
as a dictionary object, as I will show you soon.)
	Using the array sorting functions (asort or arsort) you can 
order the object without modifying the linked indexes.
	$fruits[a] = orange while $fruits[0] = "apple";
Now when trying to do the same thing as the dictionary object, you can 
have an associative array store objects.
	Let's say you want to store error information from a user-
submitted form.  (One of the Microsoft ASP examples for the dictionary 
object).
	first, define the class you wish to store.  Below is a sample 
error class.
	class Error
	{
		var $badvalue,
		     $reason,
		     $suggestions;
		function Error($errno, $Bad_Value) //default 
constructor
		{
			$this->badvalue = $Bad_Value;
			switch($errno)
			{
				case -1:
				   $this->reason = "I couldn't find 
that name in the database.";
				   $this->suggestions = "Check the 
name 
and try again or go and add it to the database.";
				break;
				case -2:
				   $this->reason = "That name is too 
long to add to the database.";
				   $this->suggestions = "Try to pick a 
name with fewer characters or make the field-size greater.";
				break;
				default:
				  $this->reason = "General Error";
				  $this->suggestions = "I don't know, 
you tell me!";
				break;
			}
		}
	}
	Then, define the class which will be the container for the 
error.  (The dictionary object)
	class Form_Errors
	{
		var $errors;
		function add_error($errno,$error)
		{
			$this->errors[$errno] = $error;  //if there is 
an errno object allready in the errors array, 
			//then it will be replaced with the same 
error.  Otherwise one will be added for you.
		}
		function get_error($errno)
		{
			return $this->errors[$errno];  //an Error 
objet 
will be returned.
		}
		function get_error($badvalue)
		{
			for($ndex = 0; $ndex < count($this->errors); 
$ndex++)
			{
				if($this->errors[$ndex]->badvalue == 
$badvalue)
					return $this->errors[$ndex];
			}
		}
		function get_error($index)
		{
			return $this->errors[$index];
		}
	}
	each of the functions I defined are simply methods of getting 
the information back out of the associative array.  The dictionary 
object in ASP comes with several already defined, but PHP allows you a 
little more flexibility in how you can retrieve the information.
as for linked lists, you can implement them in a number of ways
including using either classes or associative arrays. here is an 
example
that builds a linked list using associative arrays from a PostgreSQL
query result:
$myResult = pg_exec($SQL);
$row_count = pg_numrows($myResult);
// iterate in reverse so we always have a reference to
// the following array in the linked list ($first)
// this reference should initially be unset
for ($row = $row_count - 1; $row >= 0; $row--)
{
    list($a, $b, $c) = pg_fetch_row($myResult, $row);
    $first = array('A' => $a, 'B' => $b, 'C' => $c, 'NEXT' => $first);
}
// $first is now a reference to the head of the linked list
// let's display the list
$item = $first;
while (isset($item))
{
	printf("<P>%s %s %s</P>\n", $item['A'], $item['B'], $item
['C']);
	$item = $item['NEXT'];
}
http://www.ksa-123.com
http://www.ksa-2000.com
http://www.chat-kuwait.com
http://www.vip-kuwait.com
http://www.chat-3rb.com
http://www.vip-3rb.com
http://www.3rb-chat.com
http://www.vipgulf.com
http://www.chat-gulf.com
http://www.vip-gulf.com