How to get a value-only flat array from $wpdb->get_results when selecting a single column, without foreach()?


My query is:

$var = $wpdb->get_results("SELECT field FROM {$wpdb->prefix}table", ARRAY_A);

it returns someting like array(2) { [0]=> array(1) { ["field"]=> string(5) "test1" } [1]=> array(1) { ["field"]=> string(t) "test2" }. I.e. each item is a row with a single name-value pair.

What I want is array(2) { [0]=> string(5) "test1" [1]=> string(5) "test2" }

Currently I achieve it like this:

$var = $wpdb->get_results("SELECT field FROM {$wpdb->prefix}table");
foreach($var as $v_key => $v_val) $var[$v_key] = $v_val['field'];

Is there a shorter way to do this?

tolkodelo 2 months 0 Answers 13 views 0

Leave an answer