PHP 如何从对象中打印键值?
phpserver side programmingprogramming更新于 2025/8/18 5:07:17
假设以下是我们的对象 −
$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

