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
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
are designed to be very flexible.
(Example 1:) $fruits = array
$fruits[a] = orange while $fruits = "lemon";
This is called an associative array (Effectively the same
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 = "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
first, define the class you wish to store. Below is a sample
function Error($errno, $Bad_Value) //default
$this->badvalue = $Bad_Value;
$this->reason = "I couldn't find
that name in the database.";
$this->suggestions = "Check the
and try again or go and add it to the database.";
$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.";
$this->reason = "General Error";
$this->suggestions = "I don't know,
you tell me!";
Then, define the class which will be the container for the
error. (The dictionary object)
$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.
return $this->errors[$errno]; //an Error
will be returned.
for($ndex = 0; $ndex < count($this->errors);
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
that builds a linked list using associative arrays from a PostgreSQL
$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;
printf("<P>%s %s %s</P>\n", $item['A'], $item['B'], $item
$item = $item['NEXT'];