PHP100 helps you understand the PHP array traversal sequence

Source: Internet
Author: User
I am often asked if the PHP array is accessed using foreach, is the traversal order fixed? In what order does it traverse? For example :? Php $ arr ['laruence '] = 'huixinchen'; $ arr ['Yahoo '] = 2007; $ arr ['baidu'] = 2008; foreach ($ arras $ ke

I am often asked if the PHP array is accessed using foreach, is the traversal order fixed? In what order does it traverse?
For example:
$ Arr ['laruence '] = 'huixinchen ';
$ Arr ['Yahoo '] = 2007;
$ Arr ['baidu'] = 2008;
Foreach
($ Arr
As
$ Key => $ val)
{
// What is the result?
}

For example:
$ Arr [2] = 'huixinchen ';
$ Arr [1] = 2007;
$ Arr [0] = 2008;
Foreach
($ Arr
As
$ Key => $ val)
{
// What is the current result?
}

To fully understand this problem, I would like to first understand the internal implementation structure of the PHP array .........
PHP array in PHP, the array is implemented using a HASH structure (HashTable). PHP uses some mechanisms to enable) array addition and deletion, and linear traversal and random access are supported.
As discussed in previous articles, we have made further extensions based on the php hash algorithm.
Before learning about HashTable, let's take a look at the structure definition of HashTable. I added a comment to help you understand it:
Typedef
Struct
_ Hashtable
{
Uint
NTableSize;/* Hash size, Hash value range */
Uint
NTableMask;/* equal to nTableSize-1 for fast locating */
Uint
NNumOfElements;/* Number of actual elements in HashTable */
Ulong
NNextFreeElement;/* digital index of the next idle available location */
Bucket * pInternalPointer;/* internal position pointer, which will be reset and used by current traversal functions */
Bucket * pListHead;/* header element for linear traversal */
Bucket * pListTail;/* tail element for linear traversal */
Bucket ** arBuckets;/* Actual storage container */
Dtor_func_t
PDestructor;/* element destructor (pointer )*/
Zend_bool
Persistent;
Unsigned
Char
NApplyCount;/* loop traversal protection */
Zend_bool
BApplyProtection;
# If
ZEND_DEBUG

Int
Inconsistent;
# Endif
}
HashTable;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.