Để truy cập thành viên mảng bạn sử dụng $array['KEY'];
Để truy cập thành viên đối tượng bạn sử dụng $obj->KEY;
Để truy cập một thành viên đối tượng bên trong một mảng các đối tượng: $array[0]
// Nhận đối tượng đầu tiên trong mảng $array[0]->KEY
// sau đó truy cập khóa của nó
$array[0]
// Get the first object in the array
$array[0]->KEY
// then access its key
Bạn cũng có thể lặp qua một mảng các đối tượng như vậy:
foreach [$arrayOfObjs as $key => $object] {
echo $object->object_property;
}
Hãy nghĩ về một mảng như một bộ sưu tập của mọi thứ. Đó là một túi nơi bạn có thể lưu trữ đồ của mình và cung cấp cho họ một ID duy nhất [khóa] và truy cập chúng [hoặc lấy đồ ra khỏi túi] bằng phím đó. Tôi muốn giữ mọi thứ đơn giản ở đây, nhưng chiếc túi này cũng có thể chứa các túi khác :]
Cập nhật [điều này có thể giúp ai đó hiểu rõ hơn]:
Một mảng chứa các cặp 'key
' và '
$colorPallete = ['red', 'blue', 'green'];
0'. Cung cấp một khóa cho một thành viên mảng là tùy chọn và trong trường hợp này, nó được tự động gán một khóa số bắt đầu bằng 0 và tiếp tục tăng thêm 1 cho mỗi thành viên bổ sung. Chúng ta có thể truy xuất 'giá trị' từ mảng bằng 'key
'.Vì vậy, chúng ta có thể xác định một mảng theo các cách sau [đối với các khóa]:
Phương pháp đầu tiên:
$colorPallete = ['red', 'blue', 'green'];
Mảng trên sẽ được gán các khóa số tự động. Vì vậy, khóa được gán cho màu đỏ sẽ là 0, cho màu xanh 1, v.v.
Nhận các giá trị từ mảng trên:
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
Phương pháp thứ hai:
$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
Nhận các giá trị từ mảng trên:
$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
4 năm trước
Walter Tross ¶object, use the
$colorPallete = ['red', 'blue', 'green'];
2 statement to instantiate a class: $colorPallete = ['red', 'blue', 'green'];
3Thông tin tại Keltoi-Web dot com
19 năm trước
Ludvig dot ericson tại gmail dot com ¶object is
converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was
4, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP
7.2.0 numeric keys have been inaccessible unless iterated. $colorPallete = ['red', 'blue', 'green'];
$colorPallete = ['red', 'blue', 'green'];
516 năm trước
$colorPallete = ['red', 'blue', 'green'];
7Wyattstorch42 tại Outlook Dot Com ¶ ¶
10 năm trước
$colorPallete = ['red', 'blue', 'green'];
8$colorPallete = ['red', 'blue', 'green'];
9$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0AdityCse tại Gmail Dot Com ¶ ¶
6 năm trước
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
1$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
2$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
3$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Gabe tại fijiwebdesign dot com ¶ ¶
15 năm trước
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
5$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
6$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
017 năm trước ¶
17 năm trước
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
8$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
9$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
0$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
1$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
2Nhà phát triển Dot Amankr tại Gmail Dot Com [Aman Kuma] ¶ ¶
6 năm trước
$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
3$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
4$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
5Mithras ¶ ¶
14 năm trước
$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
6$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
7$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Brian Dot Weber1337 tại Gmail Dot Com ¶ ¶
5 năm trước
$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
9$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
0$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
1$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
2$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
3$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0mailto dot aurelian tại gmail dot com ¶ ¶
12 năm trước
$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
5cfreed tại Orange Dot Fr ¶ ¶
13 năm trước
$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
6$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
7$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
8$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
9$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Mortoray tại ecircle-ag dot com ¶
17 năm trước
$array['KEY'];
1
$array['KEY'];
2
$array['KEY'];
3
$array['KEY'];
4
$array['KEY'];
5
$array['KEY'];
6
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Qeremy [atta] gmail [dotta] com ¶ ¶
10 năm trước
$array['KEY'];
8
$array['KEY'];
9
$obj->KEY;
0
$obj->KEY;
1
$obj->KEY;
2
$obj->KEY;
3AdityCse tại Gmail Dot Com ¶ ¶
5 năm trước
$obj->KEY;
4
$obj->KEY;
5
$obj->KEY;
6mailto dot aurelian tại gmail dot com ¶ ¶
12 năm trước
$obj->KEY;
7
$obj->KEY;
8
$obj->KEY;
9
$array[0]
0
$array[0]
1cfreed tại Orange Dot Fr ¶ ¶
13 năm trước
$array[0]
2
$array[0]
3
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Mortoray tại ecircle-ag dot com ¶
5 năm trước
$array[0]
5
$array[0]
6
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0mailto dot aurelian tại gmail dot com ¶ ¶
12 năm trước
$array[0]
8
$array[0]
9
$array[0]->KEY
0
$array[0]->KEY
1
$array[0]->KEY
2
$array[0]->KEY
3
$array[0]->KEY
4
$array[0]->KEY
5
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Ludvig dot ericson tại gmail dot com ¶ ¶
16 năm trước
$array[0]->KEY
7
Wyattstorch42 tại Outlook Dot Com ¶ ¶
8 năm trước
$array[0]->KEY
8
$array[0]->KEY
9
key
0
key
1
key
2uchephilz ¶ ¶
2 năm trước
key
3
key
4
key
5
key
6
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0iblun tại gmx dot net ¶ ¶
17 năm trước
key
8
key
9
$colorPallete = ['red', 'blue', 'green'];
00$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0
Cosmitar: MHHERRERA31 tại Hotmail ¶ ¶
11 năm trước
$colorPallete = ['red', 'blue', 'green'];
02Ẩn danh ¶ ¶
12 năm trước
$colorPallete = ['red', 'blue', 'green'];
03$colorPallete = ['red', 'blue', 'green'];
04$colorPallete = ['red', 'blue', 'green'];
05$colorPallete = ['red', 'blue', 'green'];
06Spidgorny tại Gmail Dot Com ¶ ¶
13 năm trước
$colorPallete = ['red', 'blue', 'green'];
07$colorPallete = ['red', 'blue', 'green'];
08$colorPallete = ['red', 'blue', 'green'];
09$colorPallete = ['red', 'blue', 'green'];
10$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Trevor Blackbird> yurab.com ¶
16 năm trước
$colorPallete = ['red', 'blue', 'green'];
12$colorPallete = ['red', 'blue', 'green'];
13$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0Isaac Z. Schlueter I tại Foohack Dot Com ¶ ¶
14 năm trước
$colorPallete = ['red', 'blue', 'green'];
15$colorPallete = ['red', 'blue', 'green'];
16$colorPallete = ['red', 'blue', 'green'];
17$colorPallete = ['red', 'blue', 'green'];
18$colorPallete = ['red', 'blue', 'green'];
19$colorPallete = ['red', 'blue', 'green'];
20Ashley Dambra ¶ ¶
8 năm trước
$colorPallete = ['red', 'blue', 'green'];
21$colorPallete = ['red', 'blue', 'green'];
22$colorPallete = ['red', 'blue', 'green'];
23$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
0