How can one efficiently retrieve and display specific data from a database in PHP using arrays?

To efficiently retrieve and display specific data from a database in PHP using arrays, you can use the PDO (PHP Data Objects) extension to connect to the database, execute a query to fetch the desired data, and store the results in an array. You can then iterate through the array to display the data as needed.

<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Prepare and execute a query to fetch specific data
$stmt = $pdo->prepare("SELECT column1, column2 FROM table_name WHERE condition = :value");
$stmt->execute(['value' => $some_value]);

// Fetch the results into an array
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Display the data
foreach ($data as $row) {
    echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
}
?>