Introduction to the data structure stack (SplStack) of the php spl standard library
This article mainly introduces the data structure Stack (SplStack) of the php spl standard library. Stack is a special linear table, because it can only insert or delete elements at one end of a linear table (that is, to stack and to stack), you can refer
Stack is a special linear table, because it can only insert or delete elements at one end of the linear table (that is, the elements going into and out of the Stack)
SplStack inherits the double-stranded table (spldoublyshortlist) to implement the stack.
Class Abstract:
Simple use:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Think of the stack as an inverted Array $ Stack = new SplStack (); /** * The Difference Between Stack and double-stranded table is that IteratorMode has changed. The stack IteratorMode can only be: * (1) spldoubly1_list: IT_MODE_LIFO | spldoubly1_list: IT_MODE_KEEP (default value: save data after iteration) * (2) spldoubly1_list: IT_MODE_LIFO | spldoubly1_list: IT_MODE_DELETE (delete data after iteration) */ $ Stack-> setIteratorMode (spldoublyshortlist: IT_MODE_LIFO | spldoublyshortlist: IT_MODE_DELETE ); $ Stack-> push ('A '); $ Stack-> push ('B '); $ Stack-> push ('C '); $ Stack-> pop (); // output stack $ Stack-> offsetSet (0, 'First '); // If the index is 0, it is the last element. Foreach ($ stack as $ item ){ Echo $ item. PHP_EOL; // first } Print_R ($ stack); // test IteratorMode |