Does PHP have built-in data structures?

后端 未结 14 722
名媛妹妹
名媛妹妹 2021-01-30 00:18

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

相关标签:
14条回答
  • 2021-01-30 00:29

    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.

    0 讨论(0)
  • 2021-01-30 00:30

    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"
    
    0 讨论(0)
  • 2021-01-30 00:31

    PHP can also have an array of arrays which is called a "multidimensional array" or "matrix". You can have 2-dimensional arrays, 3-dimensional arrays, etc.

    0 讨论(0)
  • 2021-01-30 00:35

    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.

    0 讨论(0)
  • 2021-01-30 00:35

    Although this question is 8 years old I'm posting an answer because PHP 7 introduces an extension called ds providing specialized data structures as an alternative to the array.

    The ds,

    • uses the Ds\ namespace.
    • has 3 interfaces namely,Collection, Sequence and Hashable
    • has 8 classes namely, Vector, Deque,Queue, PriorityQueue, Map, Set, Stack, and Pair

    For more information checkout the Manual and also This blog post has some awesome information including benchmarks.

    0 讨论(0)
  • 2021-01-30 00:35

    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.

    0 讨论(0)
提交回复
热议问题