Hướng dẫn accessing array elements in php - truy cập các phần tử mảng trong php

Để 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.

Show

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 function
3.

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 function
3 để truy cập thuộc tính đối tượng của bạn.

echo $objectA->objectB->property;

Note:

  1. 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.

  2. 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:

  1. 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 and
    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
    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.

  2. Đừ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};
0

Nế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};
1

Có 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 function
9 và giá trị (________ 50):

echo $array{0};
2

Nế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};
3

Có 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};
4

Và 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

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};
5

echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
         // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
         // │                │                 └── 3rd Array dimension;
         // │                └──────────────────── 2d  Array dimension;
         // └───────────────────────────────────── 1st Array dimension;
6 đầu ra:

echo $array{0};
6

echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
         // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
         // │                │                 └── 3rd Array dimension;
         // │                └──────────────────── 2d  Array dimension;
         // └───────────────────────────────────── 1st Array dimension;
7 đầu ra:

echo $array{0};
7

echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
         // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
         // │                │                 └── 3rd Array dimension;
         // │                └──────────────────── 2d  Array dimension;
         // └───────────────────────────────────── 1st Array dimension;
8 đầu ra:

echo $array{0};
8

Vì 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 function
3 để 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];
1

Như 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];
2

Và đầ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.

  1. 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 (

    echo $object->property;
    
    9), dòng mới (
    echo $objectA->objectB->property;
    
    0), thẻ không gian hoặc thẻ HTML (ví dụ:
    echo $objectA->objectB->property;
    
    1,
    echo $objectA->objectB->property;
    
    2), v.v.

    Ví dụ nếu bạn nhìn vào đầu ra của

    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    7 và bạn thấy:

    //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];
    
    4

    Sau đó, bạn đang cố gắng truy cập phần tử với:

    //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];
    
    5

    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

    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    6 + Hãy xem mã nguồn của bạn! (Thay thế:
    echo $objectA->objectB->property;
    
    5)

    Và đột nhiên bạn có thể sẽ thấy những thứ như thế này:

    //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];
    
    6

    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ì

    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    7 và trình duyệt đã không hiển thị điều đó.

    Vì vậy, bây giờ nếu bạn cố gắng làm:

    //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];
    
    7

    Bạn sẽ nhận được đầu ra mong muốn của mình:

    //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];
    
    8
  2. Không bao giờ tin tưởng vào đầu ra của

    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    7 hoặc
    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    6 nếu bạn nhìn vào XML

    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ụ:

    //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];
    
    9

    Bây giờ nếu bạn sử dụng

    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    6 hoặc
    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    7, bạn sẽ thấy:

    $array[(Any expression)]
    
    0

    Vì 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

    echo $array["firstSubArray"]["SecondSubArray"]["ElementFromTheSecondSubArray"]
             // ├─────────────┘  ├──────────────┘  ├────────────────────────────┘
             // │                │                 └── 3rd Array dimension;
             // │                └──────────────────── 2d  Array dimension;
             // └───────────────────────────────────── 1st Array dimension;
    
    6 hoặc
    echo $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ụng
    //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)]
    
    1

    Và 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ệ?

Làm thế nào để bạn truy cập các yếu tố của một mảng?

Các phần tử mảng truy cập Bạn có thể truy cập một phần tử mảng bằng cách tham khảo số chỉ mục của nó.Các chỉ mục trong các mảng numpy bắt đầu bằng 0, nghĩa là phần tử thứ nhất có chỉ mục 0 và phần thứ hai có chỉ số 1, v.v.by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

Làm thế nào để bạn tạo và truy cập các yếu tố mảng trong PHP giải thích?

Trong PHP, hàm mảng () được sử dụng để tạo một mảng: mảng () ;..
Mảng được lập chỉ mục - Mảng có chỉ mục số ..
Mảng liên kết - Mảng có các khóa có tên ..
Mảng đa chiều - Mảng chứa một hoặc nhiều mảng ..

Array_Keys () được sử dụng trong PHP là gì?

Array_Keys () là một hàm tích hợp trong PHP và được sử dụng để trả về tất cả các khóa và mảng hoặc tập hợp con của các khóa.Tham số: Hàm lấy ba tham số trong đó một tham số là bắt buộc và hai tham số khác là tùy chọn.to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

Có bao nhiêu loại mảng có thể truy cập trong PHP?

Có bao nhiêu loại mảng có thể truy cập trong PHP?Về cơ bản có ba loại mảng trong php: mảng được lập chỉ mục, mảng số, mảng kết hợp và mảng đa chiều.