标准类对象上的 Foreach 循环



我正在为Wordpress设置一个调度插件,该插件利用dev.whenIwork.com的api。

我是PHP世界的新手,曾经是一个前端的人 - 虽然我已经掌握了基础知识,但这部分似乎让我感到困惑。

<?php
$wiw = new Wheniwork($wiw_token);
$shift_result = $wiw->get("shifts", array(
                                "location_id" => "106857", 
                                "start_time"  => "2014-03-05",
                                "user_id"     => "702524"
                              ));
print_r($shift_result);

这打印出来

stdClass Object
(
    [start] => Wed, 02 Apr 2014 15:03:05 -0400
    [end] => Sat, 05 Apr 2014 15:03:05 -0400
    [shifts] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 31577385
                    [account_id] => 66357
                    [user_id] => 702524
                    [location_id] => 106857
                    [position_id] => 160296
                    [site_id] => 65765
                    [start_time] => Wed, 02 Apr 2014 09:00:00 -0400
                    [end_time] => Wed, 02 Apr 2014 19:00:00 -0400
                    [break_time] => 1
                    [color] => 999999
                    [notes] => Website 9-12
Game 12-1
Break 1 - 1:30
Web Site 1:30 - 3:00
Game 3:00-4:00
Break 4-4:30
Website 4:30 - 5:30
Game 5:30 - 6:30
Answer emails, plan what to tackle on website next for last 30 min
                    [alerted] => 1
                    [linked_users] => 
                    [shiftchain_key] => 
                    [published] => 1
                    [published_date] => Fri, 28 Mar 2014 10:16:34 -0400
                    [notified_at] => Fri, 28 Mar 2014 10:16:35 -0400
                    [created_at] => Fri, 28 Mar 2014 09:56:36 -0400
                    [updated_at] => Wed, 02 Apr 2014 07:01:26 -0400
                    [acknowledged] => 0
                    [is_open] => 
                    [actionable] => 
                    [block_id] => 0
                )

        )
    [users] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 702524
                    [account_id] => 66357
                    [role] => 3
                    [first_name] => First
                    [last_name] => Name
                    [avatar] => stdClass Object
                        (
                            [url] => avatarurl
                            [size] => %s
                        )
                    [is_hidden] => 
                    [is_private] => 1
                    [is_deleted] => 
                    [has_email] => 1
                    [positions] => Array
                        (
                            [0] => 160296
                        )
                    [locations] => Array
                        (
                            [0] => 106857
                        )
                    [position_rates] => Array
                        (
                        )
                    [position_quality] => stdClass Object
                        (
                            [160296] => 3
                        )
                    [sort] => stdClass Object
                        (
                            [106857] => 0
                        )
                )
        )
    [locations] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 106857
                    [account_id] => 66357
                    [is_default] => 0
                    [name] => Place of Employment
                    [sort] => 0
                    [max_hours] => 0
                    [address] =>    Address of Employment
                    [coordinates] => Array
                        (
                        )
                    [latitude] => 0
                    [longitude] => 0
                    [ip_address] => 
                    [created_at] => Thu, 13 Mar 2014 07:22:28 -0400
                    [updated_at] => Thu, 13 Mar 2014 12:22:28 -0400
                    [is_deleted] => 
                )
        )
    [positions] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 160296
                    [account_id] => 66357
                    [name] => Team Member
                    [color] => 
                    [sort] => 0
                    [created_at] => Fri, 28 Feb 2014 14:52:58 -0500
                    [updated_at] => Fri, 28 Feb 2014 14:52:58 -0500
                    [is_deleted] => 
                )
        )
    [sites] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 65765
                    [account_id] => 66357
                    [location_id] => 0
                    [name] => Place of Employment
                    [color] => 
                    [description] => 
                    [address] => 
                    [coordinates] => Array
                        (
                        )
                    [latitude] => 0
                    [longitude] => 0
                    [created_at] => Fri, 28 Feb 2014 14:59:34 -0500
                    [updated_at] => Fri, 28 Feb 2014 14:59:45 -0500
                    [is_deleted] => 
                )
        )
)

我可以通过做类似的事情单独可以毫无问题地确定位置、开始时间等

$location = $shift_result->shifts[0]->location_id;

但我只是没有知识让它"自动化"。

我最终希望它像

<div id="username">
username
</div>
<div id= "starttime">
start time
</div>

等。

但这将适用于多个员工、多个班次、多个日期等。我有点头晕目眩。

我当然会继续研究,如果我自己解决这个问题,我会让你们知道。

foreach($shift_result->shifts as $shifts){
    echo $shifts->first_name;  
         }
$shift_result->shifts是一个

对象数组,所以你可以像这样迭代它:

foreach($shift_results->shifts as $shift){
    echo '<p>name: ' . $shift->first_name . '</p>';
}

相关内容

  • 没有找到相关文章

最新更新