Xóa phần tử khỏi đối tượng stdclass php

Đ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

var_dump($result->context);

Trong trường hợp có các định danh như, bạn nên thực hiện

var_dump($result->{'from-date'});

Trong trường hợp bạn định có một mảng, hãy chạy mã bên dưới

$result = json_decode($json, true);

Ngoài ra, bạn có thể chuyển đối tượng thành một mảng như vậy

$result = (array) json_decode($json);

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à

  • Nó truy cập trực tiếp các thành viên bằng cách gọi họ
  • Nó thuận tiện cho các đối tượng động
  • Nó được sử dụng để thiết lập các thuộc tính động, v.v.

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

  • stdClass truy cập trực tiếp các thành viên bằng cách gọi họ
  • Nó rất hữu ích trong đối tượng động
  • Nó được sử dụng để thiết lập các thuộc tính động, v.v.

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
var_dump($result->{'from-date'});
62
stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
3
var_dump($result->{'from-date'});
64
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
var_dump($result->{'from-date'});
67
stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
3
var_dump($result->{'from-date'});
69

$result = json_decode($json, true);
40

$result = json_decode($json, true);
41

$result = json_decode($json, true);
42

$result = json_decode($json, true);
43
stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
6______240

$result = json_decode($json, true);
46

đầu ra.

$result = (array) json_decode($json);
0

chươ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

$result = json_decode($json, true);
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
var_dump($result->{'from-date'});
64
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
)
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

$result = json_decode($json, true);
43
stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
30
$result = json_decode($json, true);
40

$result = json_decode($json, true);
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

$result = json_decode($json, true);
41

stdClass 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
var_dump($result->{'from-date'});
62
stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
3
var_dump($result->{'from-date'});
64
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
var_dump($result->{'from-date'});
67
stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
3
var_dump($result->{'from-date'});
69

$result = json_decode($json, true);
40

$result = json_decode($json, true);
41

stdClass 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

$result = json_decode($json, true);
46

đầu ra.

var_dump($result->{'from-date'});
6

chươ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

$result = json_decode($json, true);
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
var_dump($result->{'from-date'});
64
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
)
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

$result = json_decode($json, true);
41

stdClass 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

$result = json_decode($json, true);
41

stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)
37

$result = json_decode($json, true);
43____430
$result = json_decode($json, true);
40

$result = json_decode($json, true);
46

đầu ra.

$result = json_decode($json, true);
4

PHP 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