Foreach learning problems (php Brothers)

Source: Internet
Author: User
Tags array sort
The code in foreach's learning problems (php Brothers) is as follows: PHPcode & lt ;? Php $ contact = array (1,14 = & gt; & quot; Gao & quot;, & quot; company A & quot;, & quot; Beijing & quot ;, 14 = & gt; & quot; (010) 98765432 & qu foreach learning problems (php Brothers)
The code in the book is as follows:
PHP code
  
  "Gao", "A company", "Beijing", 14 => "(010) 98765432", "gao@php.com"); $ num = 0; foreach ($ contact as $ value) {echo "in the array \ $ contact ". $ num. "Element: $ value
"; $ Num ++ ;}?>

The output result is as follows:
The 0th elements in the array $ contact are: 1
The first element in the array $ contact is: (010) 1st
The 2nd elements in the array $ contact are: Company
The 3rd elements in the array $ contact are: Beijing
The 4th elements in the array $ contact are: gao@php.com

The younger brother is confused about the keys and values of array arrays.
The first element is 1. I understand why the first element jumps to the phone number?
14 => "gao .. Why does the company's Beijing mailbox jump?

Ps: the output statement of this book is actually [the $ num element in the array \ $ contact is: $ value
] None of the connectors you don't have.

------ Solution --------------------
Array running mechanism.
Press the key value from left to right to generate the content. If no key value exists, the key value is generated in numerical order.

Above
In left-to-right scenarios, there are two keys with a key value of 14

Array sort by insert
[0] = 1 (1)
[14] = Gao (2) <-(010) 98765432 (4) overwrite, but the array Insertion sorting is 2nd, so it is still 2
[15] = Company A (3)
------ Solution --------------------
Dizzy, you have already defined
Array (
14 => "gao ",
14 = & gt; "(010) 98765432"
);
Naturally, it was covered by (010) 98765432.
However, 14 => "gao" is arranged according to the array, which is originally ranked second,
So 14 => "(010) 98765432" ranked second
------ Solution --------------------
PHP code
$ Contact = array (1, 14 => "gao", "A company", "Beijing", 14 => "(010) 98765432", "gao@php.com "); // format and print the image as follows: echo'
'; Print_r ($ contact);/* Array ([0] => 1 [14] => (010) 98765432 [15] => A company [16] => Beijing [17] => gao@php.com) * // you can see the following "(010) 98765432 "overwrites the previous" gao ". why? Because php does not allow the same key in the same dimension array, you can view the key in the adult ID card number. The value is the name of a person, and the name is the same, but the ID card number is not the same. After the array becomes as follows: $ contact = array (98765432 => "(010)", "A company", "Beijing", "gao@php.com "); // at this time because the three "A company", "Beijing", "gao@php.com" are behind key 14, then their keys will not start from 1, then start 14, that is, 15 // Let's look at another example (move company A to the end of company 1): $ contact = array (1, "Company ", 14 => "(010) 98765432", "Beijing", "gao@php.com"); echo'
'; Print_r ($ contact); // The output result is as follows:/* Array ([0] => 1 [1] => Company A [14] => (010) 98765432 [15] => Beijing [16] => gao@php.com )*/

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.