Giả sử sau đây là đối tượng của chúng tôi -
$employeeDetails = (object) [ 'firstName' => 'John', 'lastName' => 'Doe', 'countryName' => 'US' ];
Chúng tôi muốn kết quả đầu ra sau đây, tức là chỉ các khóa -
firstName lastName countryName
Để chỉ hiển thị các khóa từ một đối tượng, hãy sử dụng array_keys () trong PHP.
Ví dụ
<!DOCTYPE html> <html> <body> <?php $employeeDetails = (object) [ 'firstName' => 'John', 'lastName' => 'Doe', 'countryName' => 'US' ]; $allKeysOfEmployee = array_keys((array)$employeeDetails); echo "All Keys are as follows=","<br>"; foreach($allKeysOfEmployee as &$tempKey) echo $tempKey,"<br>"; ?> </body> </html>
Đầu ra
All Keys are as follows= firstName lastName countryName