PHP array_push() 来创建一个关联数组?
要在 PHP 中创建关联数组,请使用 [] 括号。你不需要使用 array_push()。
示例
<!DOCTYPE html> <html> <body> <?php $emp= (object) [ 'employeeId'=>"101", 'employeeFirstName'=>"John", 'employeeLastName'=>"Doe", 'employeeCountryName'=>"AUS" ]; $employeeDetails[] = [ 'emp_id' => $emp->employeeId, 'emp_first_name' => $emp->employeeFirstName, 'emp_last_name' => $emp->employeeLastName, 'emp_country_name' => $emp->employeeCountryName ]; print_r(array_values($employeeDetails)); ?> </body> </html>
输出
Array ( [0] => Array ( [emp_id] => 101 [emp_first_name] => John [emp_last_name] => Doe [emp_country_name] => AUS ) )
广告