views:

3847

answers:

8

I'm looking at the PHP Manual, and I'm not seeing a section on data structures that most languages have, such as lists and sets. Am I just blind or does PHP not have anything like this built in?

+2  A: 

PHP has arrays which are actually associative arrays and can also be used as sets. Like many interpreted languages, PHP offers all this under one hood instead of providing different explicit data types.

E.g.

$lst = array(1, 2, 3);
$hsh = array(1 => "This", 2 => "is a", 3 => "test");

/Edit: Also, take a look in the manual.

Konrad Rudolph
+7  A: 

The only native data structure in PHP is array. Fortunately, array are quite flexible and can be used as hash tables as well.

http://www.php.net/array

However, there is SPL which is sort of a clone of C++ STL.

http://www.php.net/manual/en/book.spl.php

Vincent
PHP array is actually ordered dictionary. It can be used to represent arrays, lists, queues, stacks, trees, with use of references even graphs. What is more it's fast. What more could you want?
Kamil Szot
A: 

Hmm. I know about associative arrays, a little anyway. I'll have to look into them a little more to mimic sets, as I have an array (non-associative) of search strings used on a web site. I guess what I need to turn it into an associative array of the type (search string) => (number of uses).

Thanks.

If I could, upmods to Vincent and Konrad!

Thomas Owens
A: 

PHP's array doubles as both a list and a dictionary.

$myArray = array("Apples", "Oranges", "Pears");
$myScalar = $myArray[0] // == "Apples"

Or to use it as an associative array:

$myArray = array("a"=>"Apples", "b"=>"Oranges", "c"=>"Pears");
$myScalar = $myArray["a"] // == "Apples"
Corey
A: 

I think you might want to be a bit more specific, when you say data structures my mind goes in a few directions...

Arrays - They are certainly well documented and available in. (http://us.php.net/manual/en/book.array.php)

SQL Data - Depends on the database you are using, but most are available. (http://us.php.net/manual/en/book.mysql.php)

OOP - Depending on the version objects can be designed and implemented. (http://us.php.net/manual/en/language.oop.php) I had to search for OOP to find this on the php site.

Hope that helps, sorry if it does not.

Markus
+1  A: 

Of course PHP has data structures. The array in php is incredibly flexible. Some examples:

$foo = array(
  'bar' => array(1,'two',3),
  'baz' => explode(" ", "Some nice words")
);

Then you have an absolute plethora of array functions available to map/filter/walk/etc the structures, or convert, flip, reverse, etc.

mercutio
+2  A: 

The associative array can be used for most basic data structures hashtable, queue, stack. But if you want something like a tree or heap I don't think they exist by default but I'm sure there are free libraries anywhere.

To have an array emulate a stack use array_push() to add and array_pop() to take off

To have an array emulate a queue use array_push() to enqueue and array_shift() to dequeue

An associative array is a hash by default. In PHP they are allowed to have strings as indexes so this works as expected:

$array['key'] = 'value';

Finally, you can kind of emulate a binary tree with an array with the potential to have wasted space. Its useful if you know you're going to have a small tree. Using a linear array, you say for any index (i) you put its left child at index (2i+1) and right child at index (2i+2).

All of these methods are covered nicely in this article on how to make JavaScript arrays emulate higher level data structures.

Joseph Pecoraro
A: 

The reason I landed on this page was a query about "struct" (c like) for php. The examples here give me a sense of how to accomplish that