I'm sure this has been done before many times but I'm having trouble finding the simple solution, probably because it's hard for me to wrap my mind around it. The context is a custom PDO wrapper.
Summary: I want to produce a two-level (?) array schema
to easily access a MySQL table schema in two ways:
- Column type/null/defaults by column name (something like `schema['fieldname']['null']).
- Array of fields (maybe produced by
array_keys(schema)
) to use with array_intersect to validate query fields.
Details:
$schema = $pdo->run("DESCRIBE " . $tablename);
(magic happens here)
// validate input field array by field names
$queryfields = array_intersect(array_keys($schema), $inputdata;
// check format of input data
foreach ($schema as $column => $key) {
if ($column['type'] == 'datetime') {
$inputdata[$key] = date_format($inputdata[$key], 'yyyy-mm-dd');
}
}
Don't get too bogged down in the details. Basically I want to turn a flat array of arrays into the same thing but grouped by the column name.