1

When querying the database using wpdb class in WordPress I often get a numerical array of objects:

array(
    [0] => stdClass(
        comment_ID = 3
        comment_post_ID = 19
        user_id = 7
    )
    [1] => stdClass(
        comment_ID = 5
        comment_post_ID = 19
        user_id = 6
    )
)

I require to perform a second query using the user_id. To retrieve the user_id I use a foreach loop like:

$user_ids = array();

foreach($array as $object) {
    $user_ids[] = $object->user_id;
}

I want to know whether there is a PHP native better way of retrieving the user_id and avoid the foreach altogether?

John
  • 1,178
  • 5
  • 20
  • 36
  • You could try using a different query in the first instance. But as you have nto given us that query, we can't be sure – Anigel Jul 29 '13 at 10:38
  • http://php.net/array_map – BlitZ Jul 29 '13 at 10:38
  • Bind in to the DB results using a `while` loop? Without seeing how you're using the DB class, it's difficult to provide an accurate answer really. – BenM Jul 29 '13 at 10:39
  • Why avoid such a beauty like `foreach`? –  Jul 29 '13 at 10:39
  • 2
    There's nothing wrong with that loop... It's probably the fastest and most elegant way you can do this in PHP. – rid Jul 29 '13 at 10:40
  • 1
    You should consider using something like `INNER JOIN` to form query that would return list with required data so you wouldn't need to send multiple queries to the database which will be faster as well as less loops are needed. – insanebits Jul 29 '13 at 10:47
  • Well to everyone — The problem is that wpdb class returns data in the format mentioned when there are multiple rows involved. Unfortunately I cannot alter that unless I do not use native WordPress API. At times I need to re-run the loop after obtaining the second query. Two loops for the same thing is just unnecessary IMO. Modifying query to include two tables could possibly done but then that would drop query performance. – John Jul 29 '13 at 10:54

1 Answers1

4

You may try to use array_map instead of foreach:

$user_ids = array_map(function($obj){ return $obj->user_id; }, $array);

NOTE: This example requires at least PHP 5.3, as it was implemented with anonymous functions.


As the benchmark shows, native foreach-loop is faster than array_map. It is more efficient, as it is a native language construction. If ignore this fact, other cycle constructions (while, for) or array_map are the only way.


But it would be really better, if you reconstruct your query, to execute it without php-processing. There are a lot of functionality like INNER JOIN, LEFT JOIN, subqueries, loops and stored procedures. It might be really faster.

BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • there should be noted that to use anonymous functions atleast PHP 5.3 is required – insanebits Jul 29 '13 at 10:50
  • This was a solution I thought of but I'm sure this is going to be slower than foreach due to added overhead of a function call. And BTW, array_map also does a loop. I'm looking for a "better (read faster) native option". – John Jul 29 '13 at 10:57
  • @CORRUPT Saw your update. Please see my comment on the question above. – John Jul 29 '13 at 11:01
  • @John yes, [foreach is faster](http://3v4l.org/Gv9n8). It is a fastest way. And you asking to avoid it. – BlitZ Jul 29 '13 at 11:14
  • @CORRUPT :) Yeah, I was essentially looking for a language construct than a function that could yield a better performance. Something that may have been introduced in later version of PHP and that I do not know of. I guess there are none. May be you want to update your answer with a note on performance, I'll select it as the answer since there are no alternatives. – John Jul 29 '13 at 11:42