在PHP中,数据结构主要通过数组、对象和一些内置的数据结构类来实现。以下是PHP中常见的数据结构:
php
$array = array("apple", "banana", "cherry");
php
$array = array("name" => "John", "age" => 30);
php
$array = array(
array("name" => "John", "age" => 30),
array("name" => "Jane", "age" => 25)
);
PHP支持面向对象编程,可以通过类定义对象。 ```php class Person { public $name; public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person("John", 30); ```
php
$collection = array("apple", "banana", "cherry");
array_push
和array_pop
函数来模拟栈。
php
$stack = array();
array_push($stack, "apple");
array_push($stack, "banana");
$fruit = array_pop($stack); // "banana"
array_push
和array_shift
函数来模拟队列。
php
$queue = array();
array_push($queue, "apple");
array_push($queue, "banana");
$fruit = array_shift($queue); // "apple"
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"); ```
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 ```
php
$hashTable = array("name" => "John", "age" => 30);
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); ```
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); ```
php
$set = array();
$set["apple"] = true;
$set["banana"] = true;
if (isset($set["apple"])) {
echo "Apple is in the set.";
}
SplPriorityQueue
类来实现优先队列。
php
$pq = new SplPriorityQueue();
$pq->insert("apple", 3);
$pq->insert("banana", 1);
$pq->insert("cherry", 2);
echo $pq->extract(); // "apple"
php
$dictionary = array("apple" => "a fruit", "banana" => "another fruit");
echo $dictionary["apple"]; // "a fruit"
可以通过对象和引用来实现双向链表。 ```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; ```
php
$bitset = 0;
$bitset |= 1 << 2; // Set bit 2
if ($bitset & (1 << 2)) {
echo "Bit 2 is set.";
}
array_push
和array_pop
函数来模拟。
php
$stack = array();
array_push($stack, "apple");
array_push($stack, "banana");
$fruit = array_pop($stack); // "banana"
array_push
和array_shift
函数来模拟。
php
$queue = array();
array_push($queue, "apple");
array_push($queue, "banana");
$fruit = array_shift($queue); // "apple"
SplPriorityQueue
类来实现优先队列。
php
$pq = new SplPriorityQueue();
$pq->insert("apple", 3);
$pq->insert("banana", 1);
$pq->insert("cherry", 2);
echo $pq->extract(); // "apple"
php
$dictionary = array("apple" => "a fruit", "banana" => "another fruit");
echo $dictionary["apple"]; // "a fruit"
可以通过对象和引用来实现双向链表。 ```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; ```
php
$bitset = 0;
$bitset |= 1 << 2; // Set bit 2
if ($bitset & (1 << 2)) {
echo "Bit 2 is set.";
}
array_push
和array_pop
函数来模拟。
php
$stack = array();
array_push($stack, "apple");
array_push($stack, "banana");
$fruit = array_pop($stack); // "banana"
array_push
和array_shift
函数来模拟。
php
$queue = array();
array_push($queue, "apple");
array_push($queue, "banana");
$fruit = array_shift($queue); // "apple"
SplPriorityQueue
类来实现优先队列。
php
$pq = new SplPriorityQueue();
$pq->insert("apple", 3);
$pq->insert("banana", 1);
$pq->insert("cherry", 2);
echo $pq->extract(); // "apple"
php
$dictionary = array("apple" => "a fruit", "banana" => "another fruit");
echo $dictionary["apple"]; // "a fruit"
可以通过对象和引用来实现双向链表。 ```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; ```
php
$bitset = 0;
$bitset |= 1 << 2; // Set bit 2
if ($bitset & (1 << 2)) {
echo "Bit 2 is set.";
}
array_push
和array_pop
函数来模拟。
php
$stack = array();
array_push($stack, "apple");
array_push($stack, "banana");
$fruit = array_pop($stack); // "banana"
array_push
和array_shift
函数来模拟。
php
$queue = array();
array_push($queue, "apple");
array_push($queue, "banana");
$fruit = array_shift($queue); // "apple"
SplPriorityQueue
类来实现优先队列。
php
$pq = new SplPriorityQueue();
$pq->insert("apple", 3);
$pq->insert("banana", 1);
$pq->insert("cherry", 2);
echo $pq->extract(); // "apple"
php
$dictionary = array("apple" => "a fruit", "banana" => "another fruit");
echo $dictionary["apple"]; // "a fruit"
可以通过对象和引用来实现双向链表。 ```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; ```
php
$bitset = 0;
$bitset |= 1 << 2; // Set bit 2
if ($bitset & (1 << 2)) {
echo "Bit 2 is set.";
}
array_push
和array_pop
函数来模拟。
php
$stack = array();
array_push($stack, "apple");
array_push($stack, "banana");
$fruit = array_pop($stack); // "banana"
array_push
和array_shift
函数来模拟。
php
$queue = array();
array_push($queue, "apple");
array_push($queue, "banana");
$fruit = array_shift($queue); // "apple"
SplPriorityQueue
类来实现优先队列。
php
$pq = new SplPriorityQueue();
$pq->insert("apple", 3);
$pq->insert("banana", 1);
$pq->insert("cherry", 2);
echo $pq->extract(); // "apple"
php
$dictionary = array("apple" => "a fruit", "banana" => "another fruit");
echo $dictionary["apple"]; // "a fruit"
可以通过对象和引用来实现双向链表。 ```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; ```
php
$bitset = 0;
$bitset |= 1 << 2; // Set bit 2
if ($bitset & (1 << 2)) {
echo "Bit 2 is set.";
}
array_push
和array_pop
函数来模拟。
php
$stack = array();
array_push($stack, "apple");
array_push($stack, "banana");
$fruit = array_pop($stack); // "banana"
array_push
和array_shift
函数来模拟。
php
$queue = array();
array_push($queue, "apple");
array_push($queue, "banana");
$fruit = array_shift($queue); // "apple"
SplPriorityQueue
类来实现优先队列。
php
$pq = new SplPriorityQueue();
$pq->insert("apple", 3);
$pq->insert("banana", 1);
$pq->insert("cherry", 2);
echo $pq->extract(); // "apple"
php
$dictionary = array("apple" => "a fruit", "banana" => "another fruit");
echo $dictionary["apple"]; // "a fruit"
可以通过对象和引用来实现双向链表。 ```php class DoublyLinkedListNode { public $data; public $next; public $prev;
public function __construct($data) {
$this->data = $data;
$this->next = null;