【问题标题】:Foreach loop on stdClass ObjectstdClass 对象上的 Foreach 循环
【发布时间】:2014-04-02 21:55:38
【问题描述】:

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

我是 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>

等等

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

我当然会继续研究,如果我自己解决了这个问题,我会告诉你们。

【问题讨论】:

    标签: php json wordpress api for-loop


    【解决方案1】:
    foreach($shift_result->shifts as $shifts){
    
        echo $shifts->first_name;  
             }
    

    【讨论】:

      【解决方案2】:

      $shift_result-&gt;shifts 是一个对象数组,所以你可以像这样迭代它:

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

      【讨论】:

      • 关闭,只是没有 '.' $shift 之前的时间段
      • @BrandonShutter 句点用于字符串连接
      • 另外,使用你的代码给了我这个错误:Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE 谢谢@user574632
      • 哦,我知道我错过了结束单引号。
      猜你喜欢
      • 2010-10-31
      • 1970-01-01
      • 2013-06-23
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2021-01-19
      • 1970-01-01
      相关资源
      最近更新 更多