สมมติว่ามีออบเจกต์ดังนี้
$employeeDetails = (object) [ 'firstName' => 'John', 'lastName' => 'Doe', 'countryName' => 'US' ];
ต้องการผลลัพธ์เป็นเฉพาะคีย์
firstName lastName countryName
เพื่อแสดงเฉพาะคีย์จากออบเจกต์ ให้ใช้ array_keys() ใน PHP
ตัวอย่างโค้ด
<!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>
ผลลัพธ์
All Keys are as follows= firstName lastName countryName