0

我有一个 stdClass 数组。该数组的输出如下:

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
    )
 )

我想做的是向这个 stdClass 添加两个变量。它们是“total_bookings”和“total_venues”;

我目前正在遍历结果,然后计数以创建总数。我想将这两个变量添加到该 stdClass 数组的末尾。

我的PHP如下:

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

但是,当我 var_dump 时,我的 Array 看起来像这样:

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
        )

)

所以我的问题是,如何将 total_venues 和 total_bookings 添加到 stdClass()?

谢谢

4

2 回答 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;
}
于 2013-07-30T10:48:37.180 回答
2

它是一个对象,您应该使用对象表示法,而不是数组表示法。count++还可以在这两个说明下更改您的移动

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

$count++;
于 2013-07-30T10:50:40.110 回答