Đoạn mã đã cho được dành riêng cho một vấn đề PHP phổ biến "không thể sử dụng đối tượng của Loại stdClass làm mảng" Đọc tiếp để tìm hiểu cách khắc phục sự cố này một cách chính xác Hàm mà chúng tôi khuyên bạn nên sử dụng là json_decode Chức năng này có khả năng trả về một đối tượng theo mặc định Dữ liệu có thể được truy cập theo cách sau
Trong trường hợp có các định danh như, bạn nên thực hiện
Trong trường hợp bạn định có một mảng, hãy chạy mã bên dưới
Ngoài ra, bạn có thể chuyển đối tượng thành một mảng như vậy
Hãy xem xét một giải pháp khác với dấu -> Vì vậy, truy cập với sự trợ giúp của -> vì nó là một đối tượng Sau đó, thay đổi mã từ Đến Bạn đây rồi Đây được coi là một lớp trống trong PHP. Nó được áp dụng để truyền các loại khác cho đối tượng. stdClass có điểm tương đồng với các đối tượng Python hoặc Java Bạn nên cân nhắc rằng stdClass không phải là lớp cơ sở. Khi một đối tượng được chuyển đổi thành một đối tượng, nó không bị thay đổi. Tuy nhiên, khi một loại đối tượng được chuyển đổi thành một thể hiện của stdClass được tạo, nó không phải là NULL. Khi nó là NULL, phiên bản mới sẽ trống Các trường hợp sử dụng chính của stdClass trong PHP là
Hàm json_decode được sử dụng để lấy một chuỗi được mã hóa JSON và chuyển đổi nó thành một biến PHP Để chuyển đổi một mảng thành đối tượng, stdClass() được sử dụng. stdClass() là một lớp trống, được sử dụng để chuyển các loại khác thành đối tượng. Nếu một đối tượng được chuyển đổi thành đối tượng, thì nó không bị sửa đổi. Tuy nhiên, nếu kiểu đối tượng được chuyển đổi/ép kiểu thì một thể hiện của stdClass được tạo, nếu nó không phải là NULL. Nếu nó là NULL, phiên bản mới sẽ trống stdClass là lớp trống trong PHP được sử dụng để chuyển các loại khác thành đối tượng. Nó tương tự như đối tượng Java hoặc Python. stdClass không phải là lớp cơ sở của các đối tượng. Nếu một đối tượng được chuyển đổi thành đối tượng, nó không bị sửa đổi. Tuy nhiên, nếu kiểu đối tượng được chuyển đổi/ép kiểu thì một thể hiện của stdClass được tạo, nếu nó không phải là NULL. Nếu nó là NULL, phiên bản mới sẽ trống công dụng
chương trình 1. Sử dụng mảng để lưu trữ dữ liệu stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )4 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )6 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )7____88 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )0 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )2 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3____44 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )7 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3____49 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 62 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 64stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 67 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 69 40 41 42 43stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )6______240 46đầu ra. 0chương trình 2. Sử dụng stdClass thay vì mảng để lưu trữ chi tiết nhân viên (thuộc tính động) stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )4 49________ 830 ________ 87 ________ 832 ________ 833 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )35____44 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )39 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )9 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )43 64stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )47 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )48 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )4 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )51 43stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 40 46đầu ra. stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best ) Ghi chú. Có thể truyền kiểu của mảng thành đối tượng và đối tượng thành mảng chương trình 3. Chuyển đổi mảng thành đối tượng stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 41stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )58 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )6 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )7____88 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )0 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )2 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3____44 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )7 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3____49 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 62 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 64stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )5 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )1 67 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 69 40 41stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )84 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )85 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )86 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )6 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )4 ________ 243 ________ 885 ________ 240 46đầu ra. 6chương trình 4. Chuyển đổi thuộc tính đối tượng thành mảng stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )3 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )4 49________ 830 ________ 87 ________ 832 ________ 833 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )35____44 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )39 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )9 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )43 64stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )47 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )48 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 41stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )28 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )29 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )31____88 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )33 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )30 stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 41stdClass Object ( [name] => John Doe [position] => Software Engineer [address] => 53, nth street, city [status] => Best )37 43____430 40 46đầu ra. 4PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này |