What you should know about PHP arrays

Source: Internet
Author: User

(1), the traversal order of the PHP array

Give me a chestnut first:

<?php$arr[' a '] = ' 123 '; $arr [' b '] = ' 456 '; $arr [' c '] = ' 789 '; foreach ($a as $val) {var_dump ($val);}

The output of this code will obviously get the following result:

So, what if it's the following code?

<?PHP$ARR[2] = ' 123 '; $arr [1] = ' 456 '; $arr [0] = ' 789 '; foreach ($a as $val) {var_dump ($val);}

The difference between these two pieces of code is that an array is an associative array, and the other array is an indexed array, then their output will be the same, the answer is the same, the second code output will be the same as the first paragraph of the result.

So, what is this for?

The reason is that the implementation of the PHP array is a large hashtable, adding elements, the elements will be inserted in the same hash element chain of the head and the tail of the linear list. That is, the elements are traversed in a linear traversal according to the order in which they are inserted, and this particular design makes it possible in PHP that when a numeric index is used, the order of the elements is determined by the sequence of additions, not the index order. In other words, the order in which the arrays are traversed in PHP is related to the addition of elements.

For a detailed explanation, please refer to Brother Bird's in-depth understanding of PHP arrays (traversal order)

(2), what kind of two associative arrays are strictly equal

Let's look at the following code:

<?php$a = Array (' 1 ' = ' a ', ' 2 ' = ' B '); $b = Array (' 2 ' = ' B ', ' 1 ' = ' a '); $c = Array (' 1 ' = ' a ', ' 2 ' = ' B '); $flag _a_equal_b = ($a = = $b) $flag _a_strict_equal_b = ($a = = = $b); $flag _a_strict_equal_c = ($a = = = = $c); Var_dump ($fla G_a_equal_b); Var_dump ($flag _a_strict_equal_b); Var_dump ($flag _a_strict_equal_c);

Running this code will result in the following:

That is, in PHP, as long as all the keys of two associative arrays, the corresponding value is consistent, then the two associative arrays are equal. However, these two associative arrays are strictly equal only if all keys of the two associative arrays are in the same order.

What you should know about PHP arrays

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.