E
E
Eugene2018-02-01 11:53:03
MySQL
Eugene, 2018-02-01 11:53:03

How to immediately get a simple array from the database?

I send a query to the database and get the following array:

$order_ids_approved = $wpdb->get_results( "SELECT `ID` FROM `avt_posts` WHERE `post_status` = 'wc-approved' AND (`post_date` BETWEEN '{$start_day}' AND '{$end_day}')" );

array(3) { [0]=> object(stdClass)#23446 (1) { ["ID"]=> string(5) "12160" } [1]=> object(stdClass)#23412 (1) { ["ID"]=> string(5) "12162" } [2]=> object(stdClass)#23444 (1) { ["ID"]=> string(5) "12163" } }

How do you get an array like this from a database?
array(3) { [0]=> string(5) "12160" [1]=> string(5) "12162" [2]=> string(5) "12163" }

Now by means of "foreach" I get an array of the desired type for further work, but it seems to me that this is not correct.
$order_ids_approved = $wpdb->get_results( "SELECT `ID` FROM `avt_posts` WHERE `post_status` = 'wc-approved' AND (`post_date` BETWEEN '{$start_day}' AND '{$end_day}')" );

$order_id_name = [];
foreach ( $order_ids_approved as $order_id_approved ) :
    $order_id_name[] = $order_id_approved->ID;
endforeach;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-02-01
@Zoten

rechecked with the OBJECT_K
parameter
an associative array will be given, the "first" keys of which will be the first value in the SQL query, in this case "ID". those. just if the same IDs are needed, then it will be possible to take them either with a foreach enumeration where $key will be our desired value, or execute the array_keys function and get a new array with keys (IDs)

A
Alexander, 2018-02-01
@SAnhPa

https://developer.wordpress.org/reference/classes/...

$order_ids_approved = $wpdb->get_results( "SELECT `ID` FROM `avt_posts` WHERE `post_status` = 'wc-approved' AND (`post_date` BETWEEN '{$start_day}' AND '{$end_day}')", ARRAY_A );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question