<p>I'm looking at the <a href="http://www.php.net/manual/en/" rel="noreferrer">PHP Manual</a>, 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?</p>

[{'answer_id': 22405, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n<p>E.g.</p>\n<pre><code>$lst = array(1, 2, 3);\n$hsh = array(1 =&gt; &quot;This&quot;, 2 =&gt; &quot;is a&quot;, 3 =&gt; &quot;test&quot;);\n</code></pre>\n<p>Also, take a look <a href="http://www.php.net/manual/en/language.types.array.php" rel="nofollow noreferrer">in the manual</a>.</p>\n'}, {'answer_id': 22407, 'author': 'Vincent', 'author_id': 1508, 'author_profile': 'https://Stackoverflow.com/users/1508', 'pm_score': 7, 'selected': True, 'text': '<p>The only native data structure in PHP is array. Fortunately, arrays are quite flexible and can be used as hash tables as well.</p>\n\n<p><a href="http://www.php.net/array" rel="noreferrer">http://www.php.net/array</a></p>\n\n<p>However, there is SPL which is sort of a clone of C++ STL.</p>\n\n<p><a href="http://www.php.net/manual/en/book.spl.php" rel="noreferrer">http://www.php.net/manual/en/book.spl.php</a></p>\n'}, {'answer_id': 22412, 'author': 'Corey', 'author_id': 1595, 'author_profile': 'https://Stackoverflow.com/users/1595', 'pm_score': 3, 'selected': False, 'text': '<p>PHP\'s <a href="http://hudzilla.org/phpwiki/index.php?title=Arrays" rel="noreferrer">array</a> doubles as both a list and a dictionary.</p>\n\n<pre><code>$myArray = array("Apples", "Oranges", "Pears");\n$myScalar = $myArray[0] // == "Apples"\n</code></pre>\n\n<p>Or to use it as an associative array:</p>\n\n<pre><code>$myArray = array("a"=&gt;"Apples", "b"=&gt;"Oranges", "c"=&gt;"Pears");\n$myScalar = $myArray["a"] // == "Apples"\n</code></pre>\n'}, {'answer_id': 22413, 'author': 'Markus', 'author_id': 2490, 'author_profile': 'https://Stackoverflow.com/users/2490', 'pm_score': 3, 'selected': False, 'text': '<p>I think you might want to be a bit more specific, when you say data structures my mind goes in a few directions...</p>\n<p>Arrays - They are certainly well documented and available in. (<a href="http://us.php.net/manual/en/book.array.php" rel="nofollow noreferrer">http://us.php.net/manual/en/book.array.php</a>)</p>\n<p>SQL Data - Depends on the database you are using, but most are available. (<a href="http://us.php.net/manual/en/book.mysql.php" rel="nofollow noreferrer">http://us.php.net/manual/en/book.mysql.php</a>)</p>\n<p>OOP - Depending on the version objects can be designed and implemented. (<a href="http://us.php.net/manual/en/language.oop.php" rel="nofollow noreferrer">http://us.php.net/manual/en/language.oop.php</a>) I had to search for OOP to find this on the php site.</p>\n'}, {'answer_id': 22416, 'author': 'mercutio', 'author_id': 1951, 'author_profile': 'https://Stackoverflow.com/users/1951', 'pm_score': 2, 'selected': False, 'text': '<p>Of course PHP has data structures. The array in php is incredibly flexible. Some examples:</p>\n\n<pre><code>$foo = array(\n  \'bar\' =&gt; array(1,\'two\',3),\n  \'baz\' =&gt; explode(" ", "Some nice words")\n);\n</code></pre>\n\n<p>Then you have an absolute plethora of array functions available to map/filter/walk/etc the structures, or convert, flip, reverse, etc.</p>\n'}, {'answer_id': 22418, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n\n<p>To have an array emulate a stack use <code>array_push()</code> to add and <code>array_pop()</code> to take off</p>\n\n<p>To have an array emulate a queue use <code>array_push()</code> to enqueue and <code>array_shift()</code> to dequeue</p>\n\n<p>An associative array is a hash by default.  In PHP they are allowed to have strings as indexes so this works as expected:</p>\n\n<pre><code>$array[\'key\'] = \'value\';\n</code></pre>\n\n<p>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).</p>\n\n<p>All of these methods are covered nicely in <a href="http://javascript-reference.info/9-javascript-tips-you-may-not-know.htm" rel="noreferrer">this article</a> on how to make JavaScript arrays emulate higher level data structures.</p>\n'}, {'answer_id': 5607426, 'author': 'Abel Perez', 'author_id': 698288, 'author_profile': 'https://Stackoverflow.com/users/698288', 'pm_score': 2, 'selected': False, 'text': "<p>You can always create your own if you don't feel PHP includes a specific type of data structure.  For example, here is a simple Set data structure backed by an Array.</p>\n<pre><code>class ArraySet\n{\n    /** Elements in this set */\n    private $elements;\n\n    /** the number of elements in this set */\n    private $size = 0;\n\n    /**\n     * Constructs this set.\n     */ \n    public function ArraySet() {\n        $this-&gt;elements = array();\n    }\n\n    /**\n     * Adds the specified element to this set if \n     * it is not already present.\n     * \n     * @param any $element\n     *\n     * @returns true if the specified element was\n     * added to this set.\n     */\n    public function add($element) {\n        if (! in_array($element, $this-&gt;elements)) {\n            $this-&gt;elements[] = $element;\n            $this-&gt;size++;\n            return true;\n        }\n        return false;\n    }\n\n    /**\n     * Adds all of the elements in the specified \n     * collection to this set if they're not already present.\n     * \n     * @param array $collection\n     * \n     * @returns true if any of the elements in the\n     * specified collection where added to this set. \n     */ \n    public function addAll($collection) {\n        $changed = false;\n        foreach ($collection as $element) {\n            if ($this-&gt;add($element)) {\n                $changed = true;\n            }\n        }\n        return $changed;\n    }\n\n    /**\n     * Removes all the elements from this set.\n     */ \n    public function clear() {\n        $this-&gt;elements = array();\n        $this-&gt;size = 0;\n    }\n\n    /**\n     * Checks if this set contains the specified element. \n     * \n     * @param any $element\n     *\n     * @returns true if this set contains the specified\n     * element.\n     */ \n    public function contains($element) {\n        return in_array($element, $this-&gt;elements);\n    }\n\n    /**\n     * Checks if this set contains all the specified \n     * element.\n     * \n     * @param array $collection\n     * \n     * @returns true if this set contains all the specified\n     * element. \n     */ \n    public function containsAll($collection) {\n        foreach ($collection as $element) {\n            if (! in_array($element, $this-&gt;elements)) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    /**\n     * Checks if this set contains elements.\n     * \n     * @returns true if this set contains no elements. \n     */ \n    public function isEmpty() {\n        return count($this-&gt;elements) &lt;= 0;\n    }\n\n    /**\n     * Get's an iterator over the elements in this set.\n     * \n     * @returns an iterator over the elements in this set.\n     */ \n    public function iterator() {\n        return new SimpleIterator($this-&gt;elements);\n    }\n\n    /**\n     * Removes the specified element from this set.\n     * \n     * @param any $element\n     *\n     * @returns true if the specified element is removed.\n     */ \n    public function remove($element) {\n        if (! in_array($element, $this-&gt;elements)) return false;\n\n        foreach ($this-&gt;elements as $k =&gt; $v) {\n            if ($element == $v) {\n                unset($this-&gt;elements[$k]);\n                $this-&gt;size--;\n                return true;\n            }\n        }       \n    }\n\n    /**\n     * Removes all the specified elements from this set.\n     * \n     * @param array $collection\n     *\n     * @returns true if all the specified elemensts\n     * are removed from this set. \n     */ \n    public function removeAll($collection) {\n        $changed = false;\n        foreach ($collection as $element) {\n            if ($this-&gt;remove($element)) {\n                $changed = true;\n            } \n        }\n        return $changed;\n    }\n\n    /**\n     * Retains the elements in this set that are\n     * in the specified collection.  If the specified\n     * collection is also a set, this method effectively\n     * modifies this set into the intersection of \n     * this set and the specified collection.\n     * \n     * @param array $collection\n     *\n     * @returns true if this set changed as a result\n     * of the specified collection.\n     */ \n    public function retainAll($collection) {\n        $changed = false;\n        foreach ($this-&gt;elements as $k =&gt; $v) {\n            if (! in_array($v, $collection)) {\n                unset($this-&gt;elements[$k]);\n                $this-&gt;size--;\n                $changed = true;\n            }\n        }\n        return $changed;\n    }\n\n    /**\n     * Returns the number of elements in this set.\n     * \n     * @returns the number of elements in this set.\n     */ \n    public function size() {\n        return $this-&gt;size; \n    }\n\n    /**\n     * Returns an array that contains all the \n     * elements in this set.\n     * \n     * @returns an array that contains all the \n     * elements in this set.\n     */ \n    public function toArray() {\n        $elements = $this-&gt;elements;\n        return $elements;   \n    }\n}\n</code></pre>\n"}, {'answer_id': 14615433, 'author': 'Frederik Krautwald', 'author_id': 883303, 'author_profile': 'https://Stackoverflow.com/users/883303', 'pm_score': 5, 'selected': False, 'text': '<p>PHP offers data structures through the Standard PHP Library (SPL) basic extension, which is available and compiled by default in PHP 5.0.0.</p>\n\n<p>The data structures offered are available with PHP 5 >= 5.3.0, and includes:</p>\n\n<h3>Doubly Linked Lists</h3>\n\n<p>A Doubly Linked List (DLL) is a list of nodes linked in both directions to each others. Iterator’s operations, access to both ends, addition or removal of nodes have a cost of O(1) when the underlying structure is a DLL. It hence provides a decent implementation for stacks and queues.</p>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/class.spldoublylinkedlist.php" rel="noreferrer">SplDoublyLinkedList class</a>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/class.splstack.php" rel="noreferrer">SplStack class</a></li>\n<li><a href="http://www.php.net/manual/en/class.splqueue.php" rel="noreferrer">SplQueue class</a></li>\n</ul></li>\n</ul>\n\n<h3>Heaps</h3>\n\n<p>Heaps are tree-like structures that follow the heap-property: each node is greater than or equal to its children, when compared using the implemented compare method which is global to the heap.</p>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/class.splheap.php" rel="noreferrer">SplHeap class</a>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/class.splmaxheap.php" rel="noreferrer">SplMaxHeap class</a></li>\n<li><a href="http://www.php.net/manual/en/class.splminheap.php" rel="noreferrer">SplMinHeap class</a></li>\n</ul></li>\n<li><a href="http://www.php.net/manual/en/class.splpriorityqueue.php" rel="noreferrer">SplPriorityQueue class</a></li>\n</ul>\n\n<h3>Arrays</h3>\n\n<p>Arrays are structures that store the data in a continuous way, accessible via indexes. Don’t confuse them with PHP arrays: PHP arrays are in fact implemented as ordered hashtables.</p>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/class.splfixedarray.php" rel="noreferrer">SplFixedArray class</a></li>\n</ul>\n\n<h3>Map</h3>\n\n<p>A map is a datastructure holding key-value pairs. PHP arrays can be seen as maps from integers/strings to values. SPL provides a map from objects to data. This map can also be used as an object set.</p>\n\n<ul>\n<li><a href="http://www.php.net/manual/en/class.splobjectstorage.php" rel="noreferrer">SplObjectStorage class</a></li>\n</ul>\n\n<p>Source: <a href="http://php.net/manual/en/spl.datastructures.php" rel="noreferrer">http://php.net/manual/en/spl.datastructures.php</a></p>\n'}, {'answer_id': 20584756, 'author': 'John Doe', 'author_id': 3072707, 'author_profile': 'https://Stackoverflow.com/users/3072707', 'pm_score': 1, 'selected': False, 'text': '<p>PHP can also have an <em>array of arrays</em> which is called a "multidimensional array" or "matrix".  You can have 2-dimensional arrays, 3-dimensional arrays, etc.</p>\n'}, {'answer_id': 39558098, 'author': 'RanjanaLK', 'author_id': 4834574, 'author_profile': 'https://Stackoverflow.com/users/4834574', 'pm_score': 5, 'selected': False, 'text': '<p>PHP 7 introduced an extension called <code>ds</code> providing specialized data structures as an alternative to the array.</p>\n<p>The <code>ds</code>,</p>\n<ul>\n<li>uses the <code>Ds\\</code> namespace.</li>\n<li>has 3 interfaces namely,<code>Collection</code>, <code>Sequence</code> and <code>Hashable</code></li>\n<li>has 8 classes namely, <code>Vector</code>, <code>Deque</code>,<code>Queue</code>, <code>PriorityQueue</code>, <code>Map</code>, <code>Set</code>, <code>Stack</code>, and <code>Pair</code></li>\n</ul>\n<p>For more information checkout the <a href="http://php.net/manual/en/book.ds" rel="noreferrer">Manual</a> and also <a href="https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd#.yqq72xmv1" rel="noreferrer">This blog post</a> has some awesome information including benchmarks.</p>\n'}]
['https://Stackoverflow.com/questions/22401', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/572/']