Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that is an stdClass. The output of that array is as follows :

Array
(
[0] => stdClass Object
    (
        [vendor_id] => 1
        [user_id] => 1
        [date_created] => 2013-06-12 16:48:38
        [date_edited] => 
        [status] => active
        [user_firstname] => Stuart
        [user_surname] => Blackett
    )
 )

What I would like to do is add two variables to this stdClass. They are "total_bookings" and "total_venues";

I currently am looping through the results and then getting a count to create the total. I would like to add those two vars to the end of that stdClass array.

My PHP is as follows :

$vendors = $this->po_model->get_all_vendors();

        $this->template->set('total_vendors', count($vendors));

        $count = 0;

        foreach($vendors as $vendor)
        {
            $count++;

            $total_venues = $this->po_model->get_count_venues($vendor->user_id);
            $total_bookings = $this->po_model->get_count_bookings($vendor->user_id);

            $vendors[$count]['total_venues'] = $total_venues;
            $vendors[$count]['total_bookings'] = $total_bookings;
        }

However, When I var_dump that my Array looks like this :

Array
(
    [0] => stdClass Object
        (
            [vendor_id] => 1
            [user_id] => 1
            [date_created] => 2013-06-12 16:48:38
            [date_edited] => 
            [status] => active
            [user_firstname] => Stuart
            [user_surname] => Blackett
        )

    [1] => Array
        (
            [total_venues] => 6
            [total_bookings] => 14
        )

)

So my question is, How do I add total_venues and total_bookings to that stdClass()?

Thanks

share|improve this question
    
just move ` $count++;` to the end of your forloop –  Hilmi Jul 30 '13 at 10:49

2 Answers 2

up vote 2 down vote accepted
$myArray[$indexOfObject]->total_venues = 6;
$myArray[$indexOfObject]->total_bookings= 14;

Your example:

foreach($vendors as $key => $vendor)
{
    $total_venues = $this->po_model->get_count_venues($vendor->user_id);
    $total_bookings = $this->po_model->get_count_bookings($vendor->user_id);

    $vendors[$key]->total_venues = $total_venues;
    $vendors[$key]->total_bookings = $total_bookings;
}
share|improve this answer
    
Excellent. So it was an object not an notations. As @devZer0 said too. Thanks –  StuBlackett Jul 30 '13 at 10:51
1  
@StuBlackett You were also incrementing your counter at the beginning of each loop. –  Wayne Whitty Jul 30 '13 at 10:53
    
That explains the [1] array element then, Cheers –  StuBlackett Jul 30 '13 at 10:54

its an object, you should use object notations, not array notations. also change your move your count++ below these two instructions

$vendors[$count]->total_venues = $total_venues;
$vendors[$count]->total_bookings = $total_bookings;

$count++;
share|improve this answer

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.