[英]Creating an associative array from Wordpress $wpdb->get_results
It's been a long day and for some reason this is totally eluding me... 这是漫长的一天,由于某种原因,这完全使我无法理解...
My Wordpress database request looks like this: 我的Wordpress数据库请求如下所示:
$results = $wpdb->get_results($sql);
And the output looks like this 输出看起来像这样
Array ( [0] => stdClass Object ([id] => 2 [organisation] => Company 2 )
[1] => stdClass Object ([id] => 1 [organisation] => Company 1 )
)
I need to turn it into an array that looks like this: 我需要将其变成一个看起来像这样的数组:
Array ([1] => Company 1, [2] => Company 2)
This must be dead easy, but I just can't see it for some reason...Grateful for any pointers... 这一定是很简单的,但是由于某种原因我看不到它。感谢任何指针...
Why dont you just request as an array in the first place 您为什么不首先请求数组
$results = $wpdb->get_results($sql,ARRAY_A);
return will be an array. return将是一个数组。 if its one level deeper than you want, you can just do 如果它的层次比您想要的深,您可以做
$myarray = $results[0];
and you have it. 你有它。
update: per Chris Sprauge's comment: ARRAY_A is a WP constant which you have to give as it is. 更新:根据Chris Sprauge的评论:ARRAY_A是一个WP常量,必须按原样提供。 It is not an associative array parameter. 它不是关联数组参数。
$array = array();
foreach ($results as $res){
$array[] = $res->organisation;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.