2

It's been a long day and for some reason this is totally eluding me...

My Wordpress database request looks like this:

$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...

2 Answers 2

10

Why dont you just request as an array in the first place

$results = $wpdb->get_results($sql,ARRAY_A);

return will be an array. 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. It is not an associative array parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

FYI for anybody wondering what ARRAY_A is ... it's a WordPress constant, not an array you have to pass
0
$array = array();
foreach ($results as $res){
    $array[] = $res->organisation;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.