Skip to content
PHP Logo

How can the array_keys function be utilized to extract column names from an associative array in PHP?

To extract column names from an associative array in PHP, you can use the array_keys function. This function returns an array containing the keys of the input associative array, which in this case would be the column names. By using array_keys, you can easily extract the column names and store them in a separate array for further processing.

php
<?php
// Sample associative array representing a table with columns
$data = array(
    "id" => 1,
    "name" => "John Doe",
    "age" => 30
);

// Extract column names using array_keys
$columnNames = array_keys($data);

// Print out the extracted column names
print_r($columnNames);
?>