In PHP, when using mysqli extension for database operations, we often need to further process the query results. The mysqli_result object stores all data returned from the query in the database. Usually we need to traverse each row of data to perform certain operations. A common way is through loops, but if we want to use a more concise way, we can batch process each row of data with the help of PHP's built-in array_map() function.
array_map() is a higher-order function that allows you to apply a callback function to each element in the array, returning a new array. Simply put, it modifies each element in the array through a callback function.
The basic syntax is as follows:
array_map(callable $callback, array $array, array ...$arrays): array
callback is the callback function we want to apply.
array is the array we want to process.
Multiple arrays can be accepted as parameters, but in general we only pass in one array.
When processing mysqli_result , first we need to convert it into an array. It can be implemented through the mysqli_fetch_all() function, which converts the query results into a two-dimensional array. Then we can use array_map() to process each row.
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "root", "password", "database_name");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute a query
$result = $mysqli->query("SELECT * FROM users");
// Convert query results to a two-dimensional array
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Define a callback function,Used to process each row of data
function processRow($row) {
// For example,Modify the data of each row(Here is just an example)
$row['email'] = str_replace('@old-domain.com', '@m66.net', $row['email']);
return $row;
}
// use array_map Batch processing of each row
$processedData = array_map('processRow', $data);
// Output processed data
print_r($processedData);
// Close the database connection
$mysqli->close();
?>
Connect to the database : First, we establish a database connection through new mysqli() .
Execute query : Use mysqli->query() to execute the query statement and get mysqli_result from the database.
Convert query results to array : Use mysqli_fetch_all() to convert the query results into an associative array ( MYSQLI_ASSOC ).
Define callback function : processRow() is a custom callback function in which we modify the data of each row. For example, here we replace the domain name in the email from old-domain.com to m66.net via str_replace() .
Batch processing : Batch processing of each row of data through the array_map() function, and pass it into the callback function processRow to process each row.
Output processed data : Finally, we print out the processed data.
Compared to using foreach loops to process data line by line, array_map() has a cleaner syntax, making the code more compact and clear. Especially when dealing with more complex operations, using array_map() can make the code look more functional programming style.
array_map() returns a new array and does not modify the original array. So if you need to keep the original data, you can store the return value in another variable.
mysqli_fetch_all() returns a two-dimensional array, so we can handle it directly in array_map() . If you only need to process a single column, you can use other functions such as array_column() to extract the column first and then process it.
By using the array_map() function, we can batch process each row in the mysqli_result query result concisely and efficiently. It is simpler than traditional foreach loops and can clearly express the intent of data processing. I hope this method can help you better process the data in database query results.