Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a databse class which returns an array of objects. I am trying to get multiple bits of data from this single query, in order to prevent having to run three queries.

I have stumbled upon array_count_values() [ http://uk3.php.net/manual/en/function.array-count-values.php ] which will be perfect for catching various bits of data about the result set which I can use. Although I can't figure out how to cast all the second dimensions into arrays without having to just foreach through the whole return which would be a little bad I think.

Does anyone have any ideas how I can convert my array of objects, as a whole into an array, to allow this game changing function to do it's magic?

Array
(
    [0] => stdClass Object
        (
            [message_id] => 23185
            [from_profile] => 3165
            [to_profile] => 962
            [parent_message_id] => 17111
            [date_sent] => 2011-02-23 05:48:25
            [subject] => RE: hi
        )
// etc

There is also the issue that I've just thought of whilst typing this question, sods law eh? That will the function be able to parse multiple dimensions?

share|improve this question
What property from the objects are your counting? Or are you not counting values from the objects? – PhpMyCoder Mar 1 '11 at 12:11
I'm not sure, but do you mean something like array_walk_recusrive? – Zirak Mar 1 '11 at 12:14

4 Answers

up vote 1 down vote accepted

Since you only want to alter the first dimension, you can use array_walk

$dbResultsArray = // db results here

array_walk($dbResultsArray, function(&$elem) { $elem = (array)$elem; });

$dbResultsArray is passed by reference so once array_walk has run your $dbResultsArray is already updated.

I'm using closures which require php 5.3+ but you can use create_function in older versions.

share|improve this answer
Yep, this seems to be okay, as it will allow me to convert all the elements. Although not perfect as it's still looping. – DavidYell Mar 1 '11 at 13:45

This might be able to point you in the right direction;

http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass

As far as muliple dimensions go, I'm not sure on that one. Hopefully the above is helpful though.

share|improve this answer

I am not much clear what you are talking about but there is a function which can count array elements as well as object properties.

count()

Hence you do not need to convert object into array for this purpose.

share|improve this answer
I don't want to count the array, but what's in the array. – DavidYell Mar 1 '11 at 13:44

do a for loop on your array of objects, and typecast them as (array) and assign them back, you won't need to go inside further nesting,it will only typecase topmost level.

eg;

$newArr = array();
foreach($myObjArr as $t){
  $arr = (array)$t;
  $newArr[] = $arr;
}
share|improve this answer
A good suggestion, but I'd rather not foreach a loop which could be about 300 odd results. – DavidYell Mar 1 '11 at 13:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.