What are some alternative methods to storing and retrieving associative arrays from a PostgreSQL database in PHP?

Storing and retrieving associative arrays from a PostgreSQL database in PHP can be done using JSON encoding and decoding. This allows you to store complex data structures in a single column in the database and retrieve them as associative arrays in PHP.

// Storing associative array in PostgreSQL database
$data = ['key1' => 'value1', 'key2' => 'value2'];
$jsonData = json_encode($data);
$query = "INSERT INTO table_name (data_column) VALUES ('$jsonData')";
$result = pg_query($connection, $query);

// Retrieving associative array from PostgreSQL database
$query = "SELECT data_column FROM table_name WHERE id = 1";
$result = pg_query($connection, $query);
$row = pg_fetch_assoc($result);
$retrievedData = json_decode($row['data_column'], true);

// Accessing values in the retrieved associative array
echo $retrievedData['key1']; // Output: value1
echo $retrievedData['key2']; // Output: value2