简体   繁体   中英

Insert two or multiple query results in the same array

I am using wordpress code to query results from my database:

$query = $wpdb->get_results("SELECT pic0,bio,url,site,applet FROM ".$table." WHERE $condition"); // this code returns an array.

Is there a way to merge two or multiple query results in the $query array?

I have tried this:

$query = array();

$query = $wpdb->get_results("query 1");

$query = $wpdb->get_results("query 2");

And its not working.

You were actually pretty close, because you set $query again, you've actually overridden the variable, instead, you should add to it, there are 2 ways of doing so:

$query = array();

array_push($query, $wpdb->get_results("query 1"));

array_push($query, $wpdb->get_results("query 2"));

Or the shorter version:

$query = array();

$query[] = $wpdb->get_results("query 1");

$query[] = $wpdb->get_results("query 2");

You can use array_merge function as follow,

$query1Arr, $query2Arr

array_merge($query1Arr, $query2Arr);
$resultSet = array_merge($wpdb->get_results("query 1"),
                         $wpdb->get_results("query 2"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM