假设以下是我们的对象-
$employeeDetails = (object) [ 'firstName' => 'John', 'lastName' => 'Doe', 'countryName' => 'US' ];
我们想要以下输出,即仅键-
firstName lastName countryName
要仅显示对象中的键,请在PHP中使用array_keys()。
<!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