如何从 PHP 对象中打印键?
假设以下为我们的对象 -
$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
广告