Database: Move the if statement outside of the loop.

In the foreach loop of last results, move the if statement outside of the loop. There is no need to check every element in the array for the output type. Do this once outside of the loop. For large database queries with lots of rows returned, this should improve PHP performance. 

Props spacedmonkey, Cybr, johnbillion, costdev, joemcgill.
Fixes #56541.

git-svn-id: https://develop.svn.wordpress.org/trunk@56066 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-06-27 14:33:39 +00:00
parent a710b97e7c
commit 1d543a59f6

View File

@@ -3137,11 +3137,13 @@ class wpdb {
} elseif ( ARRAY_A === $output || ARRAY_N === $output ) {
// Return an integer-keyed array of...
if ( $this->last_result ) {
foreach ( (array) $this->last_result as $row ) {
if ( ARRAY_N === $output ) {
if ( ARRAY_N === $output ) {
foreach ( (array) $this->last_result as $row ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
} else {
}
} else {
foreach ( (array) $this->last_result as $row ) {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}