0

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

1
  • just move ` $count++;` to the end of your forloop Commented Jul 30, 2013 at 10:49

2 Answers 2

5
$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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Excellent. So it was an object not an notations. As @devZer0 said too. Thanks
@StuBlackett You were also incrementing your counter at the beginning of each loop.
That explains the [1] array element then, Cheers
2

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++;

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.