문제

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

도움이 되었습니까?

해결책

$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;
}

다른 팁

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++;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top