插件窝 干货文章 PHP的数据结构有哪些

PHP的数据结构有哪些

Array 数组 apple 数据结构 437    来源:    2025-03-18

在PHP中,数据结构主要通过数组、对象和一些内置的数据结构类来实现。以下是PHP中常见的数据结构:

1. 数组(Array)

  • 索引数组(Indexed Array):使用数字作为键的数组。 php $array = array("apple", "banana", "cherry");
  • 关联数组(Associative Array):使用字符串作为键的数组。 php $array = array("name" => "John", "age" => 30);
  • 多维数组(Multidimensional Array):数组中的元素也是数组。 php $array = array( array("name" => "John", "age" => 30), array("name" => "Jane", "age" => 25) );

2. 对象(Object)

  • PHP支持面向对象编程,可以通过类定义对象。 ```php class Person { public $name; public $age;

     public function __construct($name, $age) {
         $this->name = $name;
         $this->age = $age;
     }
    

    }

    $person = new Person("John", 30); ```

3. 集合(Collection)

  • PHP没有内置的集合类,但可以通过数组或自定义类来实现集合的功能。
  • 例如,使用数组模拟集合: php $collection = array("apple", "banana", "cherry");

4. 栈(Stack)

  • 栈是一种后进先出(LIFO)的数据结构。可以使用数组的array_pusharray_pop函数来模拟栈。 php $stack = array(); array_push($stack, "apple"); array_push($stack, "banana"); $fruit = array_pop($stack); // "banana"

5. 队列(Queue)

  • 队列是一种先进先出(FIFO)的数据结构。可以使用数组的array_pusharray_shift函数来模拟队列。 php $queue = array(); array_push($queue, "apple"); array_push($queue, "banana"); $fruit = array_shift($queue); // "apple"

6. 链表(Linked List)

  • PHP没有内置的链表数据结构,但可以通过对象和引用来实现。 ```php class Node { public $data; public $next;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;
     }
    

    }

    $head = new Node("apple"); $head->next = new Node("banana"); ```

7. 堆(Heap)

  • PHP没有内置的堆数据结构,但可以使用SplHeap类来实现。 ```php class MyHeap extends SplHeap { protected function compare($value1, $value2) { return $value1 - $value2; } }

    $heap = new MyHeap(); $heap->insert(3); $heap->insert(1); $heap->insert(2); echo $heap->extract(); // 1 ```

8. 哈希表(Hash Table)

  • PHP的关联数组本质上就是哈希表。 php $hashTable = array("name" => "John", "age" => 30);

9. 树(Tree)

  • PHP没有内置的树数据结构,但可以通过对象和引用来实现。 ```php class TreeNode { public $data; public $children = array();

     public function __construct($data) {
         $this->data = $data;
     }
    
     public function addChild(TreeNode $node) {
         $this->children[] = $node;
     }
    

    }

    $root = new TreeNode("root"); $child1 = new TreeNode("child1"); $root->addChild($child1); ```

10. 图(Graph)

  • PHP没有内置的图数据结构,但可以通过对象和引用来实现。 ```php class GraphNode { public $data; public $neighbors = array();

     public function __construct($data) {
         $this->data = $data;
     }
    
     public function addNeighbor(GraphNode $node) {
         $this->neighbors[] = $node;
     }
    

    }

    $node1 = new GraphNode("node1"); $node2 = new GraphNode("node2"); $node1->addNeighbor($node2); ```

11. 集合(Set)

  • PHP没有内置的集合数据结构,但可以通过数组或自定义类来实现。 php $set = array(); $set["apple"] = true; $set["banana"] = true; if (isset($set["apple"])) { echo "Apple is in the set."; }

12. 优先队列(Priority Queue)

  • 可以使用SplPriorityQueue类来实现优先队列。 php $pq = new SplPriorityQueue(); $pq->insert("apple", 3); $pq->insert("banana", 1); $pq->insert("cherry", 2); echo $pq->extract(); // "apple"

13. 字典(Dictionary)

  • PHP的关联数组可以看作是字典。 php $dictionary = array("apple" => "a fruit", "banana" => "another fruit"); echo $dictionary["apple"]; // "a fruit"

14. 双向链表(Doubly Linked List)

  • 可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;
         $this->prev = null;
     }
    

    }

    $head = new DoublyLinkedListNode("apple"); $second = new DoublyLinkedListNode("banana"); $head->next = $second; $second->prev = $head; ```

15. 位集合(BitSet)

  • PHP没有内置的位集合数据结构,但可以通过整数和位操作来模拟。 php $bitset = 0; $bitset |= 1 << 2; // Set bit 2 if ($bitset & (1 << 2)) { echo "Bit 2 is set."; }

16. 堆栈(Stack)

  • 堆栈是一种后进先出(LIFO)的数据结构,可以使用数组的array_pusharray_pop函数来模拟。 php $stack = array(); array_push($stack, "apple"); array_push($stack, "banana"); $fruit = array_pop($stack); // "banana"

17. 队列(Queue)

  • 队列是一种先进先出(FIFO)的数据结构,可以使用数组的array_pusharray_shift函数来模拟。 php $queue = array(); array_push($queue, "apple"); array_push($queue, "banana"); $fruit = array_shift($queue); // "apple"

18. 优先队列(Priority Queue)

  • 可以使用SplPriorityQueue类来实现优先队列。 php $pq = new SplPriorityQueue(); $pq->insert("apple", 3); $pq->insert("banana", 1); $pq->insert("cherry", 2); echo $pq->extract(); // "apple"

19. 字典(Dictionary)

  • PHP的关联数组可以看作是字典。 php $dictionary = array("apple" => "a fruit", "banana" => "another fruit"); echo $dictionary["apple"]; // "a fruit"

20. 双向链表(Doubly Linked List)

  • 可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;
         $this->prev = null;
     }
    

    }

    $head = new DoublyLinkedListNode("apple"); $second = new DoublyLinkedListNode("banana"); $head->next = $second; $second->prev = $head; ```

21. 位集合(BitSet)

  • PHP没有内置的位集合数据结构,但可以通过整数和位操作来模拟。 php $bitset = 0; $bitset |= 1 << 2; // Set bit 2 if ($bitset & (1 << 2)) { echo "Bit 2 is set."; }

22. 堆栈(Stack)

  • 堆栈是一种后进先出(LIFO)的数据结构,可以使用数组的array_pusharray_pop函数来模拟。 php $stack = array(); array_push($stack, "apple"); array_push($stack, "banana"); $fruit = array_pop($stack); // "banana"

23. 队列(Queue)

  • 队列是一种先进先出(FIFO)的数据结构,可以使用数组的array_pusharray_shift函数来模拟。 php $queue = array(); array_push($queue, "apple"); array_push($queue, "banana"); $fruit = array_shift($queue); // "apple"

24. 优先队列(Priority Queue)

  • 可以使用SplPriorityQueue类来实现优先队列。 php $pq = new SplPriorityQueue(); $pq->insert("apple", 3); $pq->insert("banana", 1); $pq->insert("cherry", 2); echo $pq->extract(); // "apple"

25. 字典(Dictionary)

  • PHP的关联数组可以看作是字典。 php $dictionary = array("apple" => "a fruit", "banana" => "another fruit"); echo $dictionary["apple"]; // "a fruit"

26. 双向链表(Doubly Linked List)

  • 可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;
         $this->prev = null;
     }
    

    }

    $head = new DoublyLinkedListNode("apple"); $second = new DoublyLinkedListNode("banana"); $head->next = $second; $second->prev = $head; ```

27. 位集合(BitSet)

  • PHP没有内置的位集合数据结构,但可以通过整数和位操作来模拟。 php $bitset = 0; $bitset |= 1 << 2; // Set bit 2 if ($bitset & (1 << 2)) { echo "Bit 2 is set."; }

28. 堆栈(Stack)

  • 堆栈是一种后进先出(LIFO)的数据结构,可以使用数组的array_pusharray_pop函数来模拟。 php $stack = array(); array_push($stack, "apple"); array_push($stack, "banana"); $fruit = array_pop($stack); // "banana"

29. 队列(Queue)

  • 队列是一种先进先出(FIFO)的数据结构,可以使用数组的array_pusharray_shift函数来模拟。 php $queue = array(); array_push($queue, "apple"); array_push($queue, "banana"); $fruit = array_shift($queue); // "apple"

30. 优先队列(Priority Queue)

  • 可以使用SplPriorityQueue类来实现优先队列。 php $pq = new SplPriorityQueue(); $pq->insert("apple", 3); $pq->insert("banana", 1); $pq->insert("cherry", 2); echo $pq->extract(); // "apple"

31. 字典(Dictionary)

  • PHP的关联数组可以看作是字典。 php $dictionary = array("apple" => "a fruit", "banana" => "another fruit"); echo $dictionary["apple"]; // "a fruit"

32. 双向链表(Doubly Linked List)

  • 可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;
         $this->prev = null;
     }
    

    }

    $head = new DoublyLinkedListNode("apple"); $second = new DoublyLinkedListNode("banana"); $head->next = $second; $second->prev = $head; ```

33. 位集合(BitSet)

  • PHP没有内置的位集合数据结构,但可以通过整数和位操作来模拟。 php $bitset = 0; $bitset |= 1 << 2; // Set bit 2 if ($bitset & (1 << 2)) { echo "Bit 2 is set."; }

34. 堆栈(Stack)

  • 堆栈是一种后进先出(LIFO)的数据结构,可以使用数组的array_pusharray_pop函数来模拟。 php $stack = array(); array_push($stack, "apple"); array_push($stack, "banana"); $fruit = array_pop($stack); // "banana"

35. 队列(Queue)

  • 队列是一种先进先出(FIFO)的数据结构,可以使用数组的array_pusharray_shift函数来模拟。 php $queue = array(); array_push($queue, "apple"); array_push($queue, "banana"); $fruit = array_shift($queue); // "apple"

36. 优先队列(Priority Queue)

  • 可以使用SplPriorityQueue类来实现优先队列。 php $pq = new SplPriorityQueue(); $pq->insert("apple", 3); $pq->insert("banana", 1); $pq->insert("cherry", 2); echo $pq->extract(); // "apple"

37. 字典(Dictionary)

  • PHP的关联数组可以看作是字典。 php $dictionary = array("apple" => "a fruit", "banana" => "another fruit"); echo $dictionary["apple"]; // "a fruit"

38. 双向链表(Doubly Linked List)

  • 可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;
         $this->prev = null;
     }
    

    }

    $head = new DoublyLinkedListNode("apple"); $second = new DoublyLinkedListNode("banana"); $head->next = $second; $second->prev = $head; ```

39. 位集合(BitSet)

  • PHP没有内置的位集合数据结构,但可以通过整数和位操作来模拟。 php $bitset = 0; $bitset |= 1 << 2; // Set bit 2 if ($bitset & (1 << 2)) { echo "Bit 2 is set."; }

40. 堆栈(Stack)

  • 堆栈是一种后进先出(LIFO)的数据结构,可以使用数组的array_pusharray_pop函数来模拟。 php $stack = array(); array_push($stack, "apple"); array_push($stack, "banana"); $fruit = array_pop($stack); // "banana"

41. 队列(Queue)

  • 队列是一种先进先出(FIFO)的数据结构,可以使用数组的array_pusharray_shift函数来模拟。 php $queue = array(); array_push($queue, "apple"); array_push($queue, "banana"); $fruit = array_shift($queue); // "apple"

42. 优先队列(Priority Queue)

  • 可以使用SplPriorityQueue类来实现优先队列。 php $pq = new SplPriorityQueue(); $pq->insert("apple", 3); $pq->insert("banana", 1); $pq->insert("cherry", 2); echo $pq->extract(); // "apple"

43. 字典(Dictionary)

  • PHP的关联数组可以看作是字典。 php $dictionary = array("apple" => "a fruit", "banana" => "another fruit"); echo $dictionary["apple"]; // "a fruit"

44. 双向链表(Doubly Linked List)

  • 可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;

     public function __construct($data) {
         $this->data = $data;
         $this->next = null;