循环遍历php中的对象会导致错误

时间:2017-02-09 09:14:57

标签: php foreach

好的..我做了很多研究,但我一直收到错误所以我决定直接问这个问题.. 我总是将对象转换为数组,但我现在想尝试直接使用对象来保存网站速度..我得到了这个对象:

stdClass Object
(
    [date] => Wed, 8 Feb 2017 15:03:44 +0000
    [Date] => Wed, 8 Feb 2017 15:03:44 +0000
    [subject] => asdasdasd
    [Subject] => asdasdasd
    [message_id] => 
    [toaddress] => "test@hotmail.com" 
    [to] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => test@hotmail.com
                    [mailbox] => test
                    [host] => hotmail.com
                )

        )

    [fromaddress] => Jason K 
    [from] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Jason K
                    [mailbox] => JasonK
                    [host] => hotmail.com
                )

        )

    [reply_toaddress] => Jason K 
    [reply_to] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Jason K
                    [mailbox] => JasonK
                    [host] => hotmail.com
                )

        )

    [senderaddress] => Jason K 
    [sender] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Jason K
                    [mailbox] => JasonK
                    [host] => hotmail.com
                )

        )

    [Recent] =>  
    [Unseen] =>  
    [Flagged] =>  
    [Answered] =>  
    [Deleted] =>  
    [Draft] =>  
    [Msgno] =>    1
    [MailDate] =>  8-Feb-2017 16:03:25 +0100
    [Size] => 7887
    [udate] => 1486566205
)

我试图获得第一个日期(星期三,2017年2月8日),from->邮箱和大小。我设法使用以下代码获取from->邮箱:

foreach($EmailHeaders->from as $from ){
    echo $from->mailbox;
}

但我不知道如何获得其他值以及如果我尝试:

foreach($EmailHeaders as $headers){
    echo $headers->date;
}

然后它不起作用......任何人都可以向我解释这个吗?对不起,如果之前已经问过这一千次,我只是想弄清楚..

4 个答案:

答案 0 :(得分:0)

检查此http://php.net/manual/en/function.get-object-vars.php

{
"status": "success",
"id": "https://api.test.com/me",
"denormalized": {
    "https://api.test.com/me": {
        "data": {
            "user": {
               "id2": "https://api.test.com/users/20"
            },
            "sauce": "sadasdksdls",
            "session_id": "opoqwrpoqirpoewripwor"
        },
        "relations": {
            "profile": "https://api.test.com/users/20",
            "quick_chat_profile": "https://api.test.com/profile/20"
        },
        "updates": null
    }
}
}

答案 1 :(得分:0)

很简单,你不必迭代直接属性,只需使用

class Map extends Component {
  constructor () {
    super()
    this.state = { wells: [] }
  }

  componentDidMount() {
    this.props.fetchWells()
      .then(res => this.setState({ wells: res.wells }) )
  }

  render () {
    const { wells } = this.state
    return wells.length ? this.renderWells() : (
      <span>Loading wells...</span>
    )
  }
}

答案 2 :(得分:0)

要获取“日期”,您只需使用$ EmailHeaders-&gt;日期即可 你不需要foreach。它适用于“from”,因为它包含一组对象。

答案 3 :(得分:0)

你可以简单地做

$EmailHeaders->date 

OR

$EmailHeaders->Date 

从您的对象获取日期。 迭代循环时,指针已经在日期项上,因此您无法指向日期索引。它将指向日期项目下的日期索引。