From 1d543a59f61f32bfad709bf5cfe9947323b762bf Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 27 Jun 2023 14:33:39 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wpdb.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wpdb.php b/src/wp-includes/class-wpdb.php index 59a8a8ca89..900ce42971 100644 --- a/src/wp-includes/class-wpdb.php +++ b/src/wp-includes/class-wpdb.php @@ -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 ); }