What are the potential reasons for choosing to load data from an array instead of using SQL in PHP?
Loading data from an array instead of using SQL in PHP may be preferred in situations where the data is static, small in size, and does not require frequent updates. This approach can help reduce the overhead of connecting to a database and executing SQL queries, resulting in faster performance for simple data retrieval tasks.
// Sample PHP code snippet to load data from an array instead of using SQL
$data = [
['id' => 1, 'name' => 'John Doe', 'age' => 30],
['id' => 2, 'name' => 'Jane Smith', 'age' => 25],
['id' => 3, 'name' => 'Alice Johnson', 'age' => 35]
];
// Retrieve data from the array
foreach($data as $row) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Age: " . $row['age'] . "\n";
}