Home >Backend Development >PHP Tutorial >Detailed explanation of commonly used data structures in SPL in php

Detailed explanation of commonly used data structures in SPL in php

小云云
小云云Original
2018-03-30 13:19:141783browse

This article mainly shares with you a detailed explanation of the commonly used data structures of SPL in PHP. It is mainly shared with you in the form of code. I hope it can help you.

This article stack [First in, last out]

<span style="font-size:18px;">$stack = new SplStack();  
$stack->push(&#39;data1&#39;);  
$stack->push(&#39;data2&#39;);  
$stack->push(&#39;data3&#39;);  
echo $stack->pop();  
  
//输出结果为  
//data3</span><span style="font-size:24px;font-weight: bold;">  
</span>

2. Queue [First in, first out, last in, last out]

<span style="font-size:18px;">$queue = new SplQueue();  
$queue->enqueue("data1");  
$queue->enqueue("data2");  
$queue->enqueue("data3");  
echo $queue->dequeue();  
//输出结果为  
//data1</span>

3. Heap

<span style="font-size:18px;">$heap = new SplMinHeap();  
$heap->insert("data1");  
$heap->insert("data2");  
echo $heap->extract();  
//输出结果为  
//data1</span>

4. Fixed size array

<span style="font-size:18px;">$array = new SplFixedArray(5);  
$array[0]=1;  
$array[3]=3;  
$array[2]=2;  
var_dump($array);  
//输出结果为  
// object(SplFixedArray)[1]  
// public 0 => int 1  
// public 1 => null  
// public 2 => int 2  
// public 3 => int 3  
// public 4 => null</span>

Related recommendations:

JS data structure and algorithm detailed explanation of arrays and hash tables

javascript data structure and algorithm detailed explanation

PHP implements stack data structure and bracket matching

The above is the detailed content of Detailed explanation of commonly used data structures in SPL in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn