In what scenarios would it be more efficient to handle data manipulation directly in SQL rather than using PHP arrays?

In scenarios where large amounts of data need to be manipulated, it may be more efficient to handle data manipulation directly in SQL rather than using PHP arrays. This is because SQL databases are optimized for querying and manipulating data, so performing operations directly in SQL can be faster and more resource-efficient than fetching data into PHP arrays and manipulating it there. Additionally, SQL databases can leverage indexes and other optimizations to improve query performance.

// Example of handling data manipulation directly in SQL rather than using PHP arrays

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

// Update all prices in the products table by increasing them by 10%
$stmt = $pdo->prepare('UPDATE products SET price = price * 1.1');
$stmt->execute();

// Fetch all products with a price greater than $100
$stmt = $pdo->prepare('SELECT * FROM products WHERE price > 100');
$stmt->execute();
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Output the results
foreach ($products as $product) {
    echo $product['name'] . ': $' . $product['price'] . '<br>';
}