Problems with foreach Learning (php brothers)
The code in the book is as follows
PHP Code
"Gao MoU", "a Company", "Beijing",14=> "(010) 98765432", "gao@php.com"); $num = 0; foreach ($contact as $value) { echo "in array \ $contact". $num. " Elements are: $value
"; $num + +; }?>
The output results are as follows:
The No. 0 element in the array $contact is: 1
The 1th element in the array $contact is: (010) 98765432
The 2nd element in the array $contact is: Company A
The 3rd element in the array $contact is: Beijing
The 4th element in the array $contact is: gao@php.com
My little brother is confused about the key and value of array arrays.
The No. 0 element is 1, and I understand why the 1th element jumps to the phone number?
14=> "Gao MoU" how to calculate. Then is the company Beijing mailbox Why can jump do not understand seek guidance
PS: This book output statement is actually "in the array \ $contact The first $num element is: $value
"You don't have a connector.
------Solution--------------------
Array run mechanism.
The content is generated from the left-to-right key value. A key value is generated in numeric order when no key value occurs.
Above
In case of left-to-right, there are 2 keys with a key value of 14
Array now sorted by insert
[0] = 1 (1)
[14] = high (2) <-(010) 98765432 (4) overlay But array insertion sort is 2nd, so it's still in line 2.
[] = Company A (3)
------Solution--------------------
Dizzy, you're repeating the definition.
Array
14=> "Gao MoU",
14=> "(010) 98765432"
);
Naturally it was (010) 98765432, covering the
But according to the array arrangement 14=> "Gao MoU", originally ranked in the second one,
So 14=> "(010) 98765432" ranked in the second
------Solution--------------------
PHP code
$contact = Array (1, + = "Gao MoU", "Company A", "Beijing", "(010) 98765432", "gao@php.com");//format print out, as follows: Echo ' ';p Rint_r ( $contact);/*array ([0] = 1 [+] = (010) 98765432 [[+] = Company [+] = Beijing [] = gao@php.co m) *///This can be seen in the back of the "(010) 98765432" to the front of the "high" covered, this is why? Because PHP is not allowed to have the same dimension in the same key, you can consider the key as a person's identity card number, the value as a person's name, the same name and the identity card number will not be the same. The array then becomes as follows: $contact =array (1,14=> "(010) 98765432", "a Company", "Beijing", "gao@php.com");//This time because "Company A", "Beijing", "gao@php.com" These three after the key 14, then their keys will not start from 1, but then 14 start, that is, 15//we look at an example ("Company A" moved to the back of 1): $contact =array (1, "a Company",14=> "(010) 98765432" , "Beijing", "gao@php.com"), Echo ' ';p rint_r ($contact);//The result output is as follows:/*array ([0] = 1 [1] [+] a company [+] = (010) 98765432 [+] = Beijing [+] = gao@php.com) */
li>