<?php
// $this->last_result analogue in WPDB for 1000 posts' "SELECT meta_key, meta_value WHERE...."
$last_result = array_fill( 0, 2000, (object) [
'meta_key' => 'hello_who',
'meta_value' => str_repeat( 'World and WordPress ', 20 ),
] );
const ARRAY_N = 0b0;
$output = 0b0;
// Startup engines;
number_format( hrtime( true ) / 1e9 );
$new_array = [];
foreach ( (array) $last_result as $row ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
}
$new_array = [];
foreach ( (array) $last_result as $row ) {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}
$new_array = [];
// Test.
$new_array = [];
$t = hrtime( true );
foreach ( (array) $last_result as $row ) {
if ( ARRAY_N === $output ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
} else {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}
}
echo 'Old ARRAY_N: ', number_format( ( hrtime( true ) - $t ) / 1e9, 5 ).'s', "\n";
$new_array = [];
$t = hrtime( true );
if ( ARRAY_N === $output ) {
foreach ( (array) $last_result as $row ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
}
} else {
foreach ( (array) $last_result as $row ) {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}
}
echo 'New ARRAY_N: ', number_format( ( hrtime( true ) - $t ) / 1e9, 5 ).'s', "\n";
// Test ARRAY_A now.
$output = 0b1;
$new_array = [];
$t = hrtime( true );
foreach ( (array) $last_result as $row ) {
if ( ARRAY_N === $output ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
} else {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}
}
echo 'Old ARRAY_A: ', number_format( ( hrtime( true ) - $t ) / 1e9, 5 ).'s', "\n";
$new_array = [];
$t = hrtime( true );
if ( ARRAY_N === $output ) {
foreach ( (array) $last_result as $row ) {
// ...integer-keyed row arrays.
$new_array[] = array_values( get_object_vars( $row ) );
}
} else {
foreach ( (array) $last_result as $row ) {
// ...column name-keyed row arrays.
$new_array[] = get_object_vars( $row );
}
}
echo 'New ARRAY_A: ', number_format( ( hrtime( true ) - $t ) / 1e9, 5 ).'s', "\n";
preferences:
70.28 ms | 409 KiB | 5 Q