使用forlopp打印数组数据

时间:2016-08-11 09:19:51

标签: php arrays

$responses= stdClass Object (
 [status] => SUCCESS
 [value] => stdClass Object (
   [messageTaskList] => Array (
     [0] => stdClass Object (
       [id] => 19
       [userId] => 38
       [text] => some text
       [currentCount] => 0
       [finishCount] => 5000
       [createTime] => 1470223038000
       [finishTime] =>
       [status] => 1
       [requestsSent] => 0
       [url] => linked.in/searchparams=marketingits=sample )
    [1] => stdClass Object (
       [id] => 20
       [userId] => 38
       [text] => Grand Rapids
       [currentCount] => 0
       [finishCount] => 5000
       [createTime] => 1470223059000
       [finishTime] =>
       [status] => 1
       [requestsSent] => 0
       [url] => linked.in/searchparams=marketingits=sample )
    [2] => stdClass Object (
       [id] => 21
       [userId] => 38
       [text] => Grand Rapids
       [currentCount] => 0
       [finishCount] => 5000
       [createTime] => 1470223751000
       [finishTime] =>
       [status] => 1
       [requestsSent] => 0
       [url] => https://google.com ) ) )
 [action] => GET_MESSAGE_TASK_LIST
 [eventId] => )

foreach($responses as $key => $value) {
                    echo $value->id . ", " . $value->text . "<br>";
                }

我正在尝试在表中显示数组值,但是我收到了下面提到的错误。

  

注意:尝试在第238行的E:\ xampp \ htdocs \ linkedin \ all-tasks.php中获取非对象的属性

     

注意:尝试在第238行的E:\ xampp \ htdocs \ linkedin \ all-tasks.php中获取非对象的属性

2 个答案:

答案 0 :(得分:4)

idtext位于valuemessageTaskList的数组对象中,因此通过将指针放在同一个上,循环进入内部数组{{1 }和id驻留。

这应该会有所帮助:

text

答案 1 :(得分:1)

这就是你的$ response结构的样子:

$responses = stdClass (
    [status] => SUCCESS
    [value] =>
        stdClass Object (
            [messageTaskList] => Array (
                [0] => stdClass Object (
                    [id] => 19
                    [userId] => 38
                    [text] => some text
                    [currentCount] => 0
                    [finishCount] => 5000
                    [createTime] => 1470223038000
                    [finishTime] =>
                    [status] => 1
                    [requestsSent] => 0
                    [url] => linked.in/searchparams=marketingits=sample
                )
                [1] => stdClass Object (
                    [id] => 20
                    [userId] => 38
                    [text] => Grand Rapids
                    [currentCount] => 0
                    [finishCount] => 5000
                    [createTime] => 1470223059000
                    [finishTime] =>
                    [status] => 1
                    [requestsSent] => 0
                    [url] => linked.in/searchparams=marketingits=sample
                )
                [2] => stdClass Object (
                    [id] => 21
                    [userId] => 38
                    [text] => Grand Rapids
                    [currentCount] => 0
                    [finishCount] => 5000
                    [createTime] => 1470223751000
                    [finishTime] =>
                    [status] => 1
                    [requestsSent] => 0
                    [url] => https://google.com
                )
            )
        )
        [action] => GET_MESSAGE_TASK_LIST
        [eventId] =>
    )

您需要做的就是迭代正确的元素:

foreach ($responses->value->messageTaskList as $key => $value) {
    echo $value->id . ", " . $value->text . "<br>";        
}