Để truy cập
$array[[Any expression]]3 hoặc
$array[[Any expression]]4 Bạn cách sử dụng hai toán tử khác nhau.
Mảng
Để truy cập các thành phần mảng, bạn phải sử dụng
$array[[Any expression]]5.
echo $array[0];
Trên các phiên bản PHP cũ hơn, một cú pháp thay thế sử dụng
$array[[Any expression]]6 cũng được cho phép:
echo $array{0};
Sự khác biệt giữa việc khai báo một mảng và truy cập một phần tử mảng
Xác định một mảng và truy cập một phần tử mảng là hai điều khác nhau. Vì vậy, đừng trộn chúng lên.
Để xác định một mảng, bạn có thể sử dụng
$array[[Any expression]]7 hoặc cho PHP> = 5,4
$array[[Any expression]]5 và bạn gán/đặt một mảng/-Lement. Trong khi khi bạn đang truy cập vào một phần tử mảng với
$array[[Any expression]]5 như đã đề cập ở trên, bạn sẽ nhận được giá trị của một phần tử mảng trái ngược với việc đặt một phần tử.
//Declaring an array
$arrayA = array [ /*Some stuff in here*/ ];
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4
//Accessing an array element
echo $array[0];
Truy cập phần tử mảng
Để truy cập một phần tử cụ thể trong một mảng, bạn có thể sử dụng bất kỳ biểu thức nào bên trong
$array[[Any expression]]5 hoặc
$array[[Any expression]]6 sau đó đánh giá vào khóa bạn muốn truy cập:
$array[[Any expression]]
Vì vậy, chỉ cần nhận thức được biểu thức bạn sử dụng làm khóa và cách nó được giải thích bởi PHP:
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function
Truy cập mảng đa chiều
Nếu bạn có nhiều mảng trong nhau, bạn chỉ cần có một mảng đa chiều. Để truy cập một phần tử mảng trong một mảng phụ, bạn chỉ cần sử dụng nhiều
$array[[Any expression]]5.
echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
Các đối tượng
Để truy cập một thuộc tính đối tượng, bạn phải sử dụng
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function3.
echo $object->property;
Nếu bạn có một đối tượng trong một đối tượng khác, bạn chỉ cần sử dụng nhiều
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function3 để truy cập thuộc tính đối tượng của bạn.
echo $objectA->objectB->property;
Note:
Ngoài ra, bạn phải cẩn thận nếu bạn có một tên tài sản không hợp lệ! Vì vậy, để xem tất cả các vấn đề, mà bạn có thể gặp phải với một tên thuộc tính không hợp lệ, hãy xem câu hỏi/câu trả lời này. Và đặc biệt là cái này nếu bạn có số khi bắt đầu tên tài sản.
Bạn chỉ có thể truy cập các thuộc tính với khả năng hiển thị công khai từ bên ngoài lớp. Nếu không [riêng tư hoặc được bảo vệ] Bạn cần một phương thức hoặc phản xạ, mà bạn có thể sử dụng để nhận giá trị của tài sản.
Mảng & đối tượng
Bây giờ nếu bạn có các mảng và đối tượng trộn lẫn với nhau, bạn chỉ cần xem nếu bây giờ bạn truy cập một phần tử mảng hoặc thuộc tính đối tượng và sử dụng toán tử tương ứng cho nó.
//Object echo $object->anotherObject->propertyArray["elementOneWithAnObject"]->property; //├────┘ ├───────────┘ ├───────────┘ ├──────────────────────┘ ├──────┘ //│ │ │ │ └── property ; //│ │ │ └───────────────────────────── array element [object] ; Use -> To access the property 'property' //│ │ └─────────────────────────────────────────── array [property] ; Use [] To access the array element 'elementOneWithAnObject' //│ └────────────────────────────────────────────────────────── property [object] ; Use -> To access the property 'propertyArray' //└────────────────────────────────────────────────────────────────── object ; Use -> To access the property 'anotherObject' //Array echo $array["arrayElement"]["anotherElement"]->object->property["element"]; //├───┘ ├────────────┘ ├──────────────┘ ├────┘ ├──────┘ ├───────┘ //│ │ │ │ │ └── array element ; //│ │ │ │ └─────────── property [array] ; Use [] To access the array element 'element' //│ │ │ └─────────────────── property [object] ; Use -> To access the property 'property' //│ │ └────────────────────────────────────── array element [object] ; Use -> To access the property 'object' //│ └────────────────────────────────────────────────────── array element [array] ; Use [] To access the array element 'anotherElement' //└──────────────────────────────────────────────────────────── array ; Use [] To access the array element 'arrayElement'
Tôi hy vọng điều này cung cấp cho bạn một ý tưởng sơ bộ về cách bạn có thể truy cập các mảng và đối tượng, khi chúng được lồng vào nhau.
Note:
Nếu nó được gọi là một mảng hoặc đối tượng phụ thuộc vào phần ngoài cùng của biến của bạn. Vì vậy,
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function
5 là một mảng ngay cả khi nó có [lồng nhau] các đối tượng bên trong nó vàecho $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function
6 là một đối tượng ngay cả khi nó có các mảng [lồng nhau] bên trong.echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function
5 is an array even if it has [nested] objects inside of it andecho $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function
6 is an object even if it has [nested] arrays inside.Và nếu bạn không chắc chắn nếu bạn có một đối tượng hoặc mảng, chỉ cần sử dụng
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function
7.Đừng bối rối nếu ai đó sử dụng kiểu mã hóa khác hơn bạn:
//Both methods/styles work and access the same data echo $object->anotherObject->propertyArray["elementOneWithAnObject"]->property; echo $object-> anotherObject ->propertyArray ["elementOneWithAnObject"]-> property; //Both methods/styles work and access the same data echo $array["arrayElement"]["anotherElement"]->object->property["element"]; echo $array["arrayElement"] ["anotherElement"]-> object ->property["element"];
Mảng, đối tượng và vòng lặp
Nếu bạn không chỉ muốn truy cập một phần tử duy nhất, bạn có thể lặp qua mảng / đối tượng lồng nhau của mình và xem qua các giá trị của một chiều cụ thể.
Đối với điều này, bạn chỉ cần truy cập vào kích thước mà bạn muốn lặp và sau đó bạn có thể lặp lại tất cả các giá trị của kích thước đó.
Ví dụ, chúng tôi lấy một mảng, nhưng nó cũng có thể là một đối tượng:
echo $array{0};
0Nếu bạn lặp qua chiều đầu tiên, bạn sẽ nhận được tất cả các giá trị từ chiều đầu tiên:
echo $array{0};
1Có nghĩa là ở đây trong chiều đầu tiên, bạn sẽ chỉ có 1 phần tử với khóa [____ 48]
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function9 và giá trị [________ 50]:
echo $array{0};
2Nếu bạn lặp qua chiều thứ hai, bạn sẽ nhận được tất cả các giá trị từ chiều thứ hai:
echo $array{0};
3Có nghĩa là ở đây trong chiều thứ hai, bạn sẽ có 3 phần tử với các khóa [____ 48]
echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
2, echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
3, echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
4 và các giá trị [________ 50]:echo $array{0};
4Và với điều này, bạn có thể lặp qua bất kỳ chiều nào bạn muốn bất kể đó là một mảng hoặc đối tượng.
Phân tích đầu ra echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
6 / echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
7 / echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
8
echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
Tất cả 3 hàm gỡ lỗi này xuất ra cùng một dữ liệu, chỉ ở định dạng khác hoặc với một số dữ liệu meta [ví dụ: loại, kích thước]. Vì vậy, ở đây tôi muốn chỉ ra cách bạn phải đọc đầu ra của các chức năng này để biết/đi đến cách truy cập dữ liệu nhất định từ mảng/đối tượng của bạn.
Mảng đầu vào:
echo $array{0};
5echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
6 đầu ra:echo $array{0};
6echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
7 đầu ra:echo $array{0};
7echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
// ├─────────────┘ ├──────────────┘ ├────────────────────────────┘
// │ │ └── 3rd Array dimension;
// │ └──────────────────── 2d Array dimension;
// └───────────────────────────────────── 1st Array dimension;
8 đầu ra:echo $array{0};
8Vì vậy, như bạn có thể thấy tất cả các đầu ra là khá giống nhau. Và nếu bây giờ bạn muốn truy cập giá trị 2, bạn có thể bắt đầu từ chính giá trị mà bạn muốn truy cập và tìm đường đến "trên cùng bên trái".
1. Trước tiên chúng ta thấy rằng giá trị 2 nằm trong một mảng có khóa 1
echo $array{0};
9Điều này có nghĩa là chúng tôi phải sử dụng
$array[[Any expression]]5 để truy cập giá trị 2 với
echo $object->property;
3, vì giá trị có khóa/chỉ mục 1.echo $object->property;
3, since the value has the key/index 1.2. Tiếp theo chúng ta thấy rằng mảng được gán cho một thuộc tính có thuộc tính tên của một đối tượng
//Declaring an array
$arrayA = array [ /*Some stuff in here*/ ];
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4
//Accessing an array element
echo $array[0];
0Điều này có nghĩa là chúng ta phải sử dụng
echo $array[0]; //The key is an integer; It accesses the 0's element echo $array["0"]; //The key is a string; It accesses the 0's element echo $array["string"]; //The key is a string; It accesses the element with the key 'string' echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value echo $array[cOnStAnT]; //The key is also a constant and not a string echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable' echo $array[functionXY[]]; //The key will be the return value of the function3 để truy cập thuộc tính của đối tượng, ví dụ:
echo $object->property;
5.echo $object->property;
5.Vì vậy, cho đến bây giờ, chúng tôi biết rằng chúng tôi phải sử dụng
echo $object->property;
6.echo $object->property;
6.3. Và cuối cùng chúng ta thấy, rằng ngoài cùng là một mảng
//Declaring an array
$arrayA = array [ /*Some stuff in here*/ ];
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4
//Accessing an array element
echo $array[0];
1Như chúng ta biết rằng chúng ta phải truy cập một phần tử mảng với
$array[[Any expression]]5, chúng ta thấy ở đây chúng ta phải sử dụng
echo $object->property;
8 để truy cập đối tượng. Bây giờ chúng ta có thể đặt tất cả các phần này lại với nhau và viết:echo $object->property;
8 to access the object. We now can put all these parts together and write://Declaring an array
$arrayA = array [ /*Some stuff in here*/ ];
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4
//Accessing an array element
echo $array[0];
2Và đầu ra sẽ là:
//Declaring an array
$arrayA = array [ /*Some stuff in here*/ ];
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4
//Accessing an array element
echo $array[0];
3Đừng để php troll bạn!
Có một vài điều mà bạn phải biết, để bạn không dành hàng giờ cho việc tìm kiếm chúng.
Các ký tự "ẩn"
Đôi khi bạn có các ký tự trong khóa của mình, mà bạn không thấy trong cái nhìn đầu tiên trong trình duyệt. Và sau đó bạn đang tự hỏi, tại sao bạn không thể truy cập phần tử. Các ký tự này có thể là: tab [
9], dòng mới [echo $object->property;
0], thẻ không gian hoặc thẻ HTML [ví dụ:echo $objectA->objectB->property;
1,echo $objectA->objectB->property;
2], v.v.echo $objectA->objectB->property;
Ví dụ nếu bạn nhìn vào đầu ra của
7 và bạn thấy:echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
4//Declaring an array $arrayA = array [ /*Some stuff in here*/ ]; $arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4 //Accessing an array element echo $array[0];
Sau đó, bạn đang cố gắng truy cập phần tử với:
5//Declaring an array $arrayA = array [ /*Some stuff in here*/ ]; $arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4 //Accessing an array element echo $array[0];
Nhưng bạn đang nhận được thông báo:
Thông báo: Chỉ mục không xác định: Khóa
Đây là một dấu hiệu tốt cho thấy phải có một số ký tự ẩn, vì bạn không thể truy cập phần tử, ngay cả khi các phím có vẻ khá chính xác.
Bí quyết ở đây là sử dụng
6 + Hãy xem mã nguồn của bạn! [Thay thế:echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
5]echo $objectA->objectB->property;
Và đột nhiên bạn có thể sẽ thấy những thứ như thế này:
6//Declaring an array $arrayA = array [ /*Some stuff in here*/ ]; $arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4 //Accessing an array element echo $array[0];
Bây giờ bạn sẽ thấy rằng khóa của bạn có thẻ HTML trong đó + một ký tự dòng mới mà bạn đã không thấy ở nơi đầu tiên, vì
7 và trình duyệt đã không hiển thị điều đó.echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
Vì vậy, bây giờ nếu bạn cố gắng làm:
7//Declaring an array $arrayA = array [ /*Some stuff in here*/ ]; $arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4 //Accessing an array element echo $array[0];
Bạn sẽ nhận được đầu ra mong muốn của mình:
8//Declaring an array $arrayA = array [ /*Some stuff in here*/ ]; $arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4 //Accessing an array element echo $array[0];
Không bao giờ tin tưởng vào đầu ra của
7 hoặcecho $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
6 nếu bạn nhìn vào XMLecho $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
Bạn có thể có một tệp XML hoặc chuỗi được tải vào một đối tượng, ví dụ:
9//Declaring an array $arrayA = array [ /*Some stuff in here*/ ]; $arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4 //Accessing an array element echo $array[0];
Bây giờ nếu bạn sử dụng
6 hoặcecho $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
7, bạn sẽ thấy:echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
$array[[Any expression]]
0Vì vậy, như bạn có thể thấy, bạn không thấy các thuộc tính của tiêu đề. Vì vậy, như tôi đã nói, không bao giờ tin tưởng vào đầu ra của
6 hoặcecho $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
7 khi bạn có đối tượng XML. Luôn luôn sử dụngecho $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"] // ├─────────────┘ ├──────────────┘ ├────────────────────────────┘ // │ │ └── 3rd Array dimension; // │ └──────────────────── 2d Array dimension; // └───────────────────────────────────── 1st Array dimension;
//Object echo $object->anotherObject->propertyArray["elementOneWithAnObject"]->property; //├────┘ ├───────────┘ ├───────────┘ ├──────────────────────┘ ├──────┘ //│ │ │ │ └── property ; //│ │ │ └───────────────────────────── array element [object] ; Use -> To access the property 'property' //│ │ └─────────────────────────────────────────── array [property] ; Use [] To access the array element 'elementOneWithAnObject' //│ └────────────────────────────────────────────────────────── property [object] ; Use -> To access the property 'propertyArray' //└────────────────────────────────────────────────────────────────── object ; Use -> To access the property 'anotherObject' //Array echo $array["arrayElement"]["anotherElement"]->object->property["element"]; //├───┘ ├────────────┘ ├──────────────┘ ├────┘ ├──────┘ ├───────┘ //│ │ │ │ │ └── array element ; //│ │ │ │ └─────────── property [array] ; Use [] To access the array element 'element' //│ │ │ └─────────────────── property [object] ; Use -> To access the property 'property' //│ │ └────────────────────────────────────── array element [object] ; Use -> To access the property 'object' //│ └────────────────────────────────────────────────────── array element [array] ; Use [] To access the array element 'anotherElement' //└──────────────────────────────────────────────────────────── array ; Use [] To access the array element 'arrayElement'
3 để xem tệp/chuỗi XML đầy đủ.Vì vậy, chỉ cần sử dụng một trong các phương thức được hiển thị dưới đây:
$array[[Any expression]]
1Và sau đó bạn sẽ nhận được đầu ra:
$array[[Any expression]]
2
Để biết thêm thông tin, xem:
Chung [Biểu tượng, lỗi]
- Tham khảo - Biểu tượng này có nghĩa là gì trong PHP?
- Tham khảo - Lỗi này có nghĩa là gì trong PHP?
- Lỗi parse/cú pháp PHP; và làm thế nào để giải quyết chúng
Vấn đề tên tài sản
- Làm thế nào tôi có thể truy cập một thuộc tính có tên không hợp lệ?
- Làm thế nào để truy cập các thuộc tính đối tượng với các tên như số nguyên hoặc tên thuộc tính không hợp lệ?