如何循环并按顺序获取第二个数组中的数值

huangapple go评论50阅读模式
英文:

How to loop and get the values from second array in sequence

问题

这是我想要循环到Events Index并获取其中所有内容打印出来的数组。我该如何做?

以下是我正在尝试循环的数组代码:

[1] => stdClass Object
        (
            [id] => RPDNLNYO6U
            [active] => 
            [events] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => RPDNLNYO6U_1
                            [created_at] => 2023-02-06T10:55:42.501003Z
                            [visibility] => all
                            [text] => Hi
                            [author_id] => 170bdd03-a163-49e9-4295-11fae376ff0e
                        )

                    [1] => stdClass Object
                        (
                            [id] => RPDNLNYO6U_2
                            [created_at] => 2023-02-06T10:55:44.102000Z
                            [visibility] => all
                           
                            [text] => I need help
                            [author_id] => 170bdd03-a163-49e9-4295-11fae376ff0e
                            
                        )

                    [2] => stdClass Object
                        (
                            [id] => RPDNLNYO6U_3
                            [created_at] => 2023-02-06T11:06:11.002000Z
                            [visibility] => agents
                            [type] => system_message
                            [text] => Chat is idle due to 10 minutes of inactivity
                            [system_message_type] => routing.idle
                            [text_vars] => stdClass Object
                                (
                                    [duration] => 10
                                )

                        )

                    [3] => stdClass Object
                        (
                            [id] => RPDNLNYO6U_4
                            [created_at] => 2023-02-06T11:11:14.002000Z
                            [visibility] => all
                            [type] => system_message
                            [text] => Chat archived due to 15 minutes of inactivity
                            [system_message_type] => routing.archived_inactive
                            [text_vars] => stdClass Object
                                (
                                    [duration] => 15
                                )

                        )

                )

        )
英文:

This is the Array I want to loop Into Events Index and get all the context print out of it how can i do this

Here is the Array Code Which i am trying to loop

[1] => stdClass Object
(
[id] => RPDNLNYO6U
[active] => 
[events] => Array
(
[0] => stdClass Object
(
[id] => RPDNLNYO6U_1
[created_at] => 2023-02-06T10:55:42.501003Z
[visibility] => all
[text] => Hi
[author_id] => 170bdd03-a163-49e9-4295-11fae376ff0e
)
[1] => stdClass Object
(
[id] => RPDNLNYO6U_2
[created_at] => 2023-02-06T10:55:44.102000Z
[visibility] => all
[text] => I need help
[author_id] => 170bdd03-a163-49e9-4295-11fae376ff0e
)
[2] => stdClass Object
(
[id] => RPDNLNYO6U_3
[created_at] => 2023-02-06T11:06:11.002000Z
[visibility] => agents
[type] => system_message
[text] => Chat is idle due to 10 minutes of inactivity
[system_message_type] => routing.idle
[text_vars] => stdClass Object
(
[duration] => 10
)
)
[3] => stdClass Object
(
[id] => RPDNLNYO6U_4
[created_at] => 2023-02-06T11:11:14.002000Z
[visibility] => all
[type] => system_message
[text] => Chat archived due to 15 minutes of inactivity
[system_message_type] => routing.archived_inactive
[text_vars] => stdClass Object
(
[duration] => 15
)
)
)
)

答案1

得分: 0

你可以在PHP中使用foreach循环来遍历数组并获取所需的所有内容。

$array = [1 => (object) [
    'id' => 'RPDNLNYO6U',
    'active' => '',
    'events' => [
        (object) [
            'id' => 'RPDNLNYO6U_1',
            'created_at' => '2023-02-06T10:55:42.501003Z',
            'visibility' => 'all',
            'text' => 'Hi',
            'author_id' => '170bdd03-a163-49e9-4295-11fae376ff0e'
        ]
    ]
]];

foreach ($array as $object) {
    $id = $object->id;
    $active = $object->active;
    foreach ($object->events as $event) {
        $event_id = $event->id;
        $created_at = $event->created_at;
        $visibility = $event->visibility;
        $text = $event->text;
        $author_id = $event->author_id;
        echo "Event ID: $event_id, Created At: $created_at, Visibility: $visibility, Text: $text, Author ID: $author_id\n";
    }
}

这将遍历外部数组并打印出每个事件的所有内容。

英文:

You can use a foreach loop in PHP to loop through the array and get all the context you need.

$array = [1 => (object) [
'id' => 'RPDNLNYO6U',
'active' => '',
'events' => [
(object) [
'id' => 'RPDNLNYO6U_1',
'created_at' => '2023-02-06T10:55:42.501003Z',
'visibility' => 'all',
'text' => 'Hi',
'author_id' => '170bdd03-a163-49e9-4295-11fae376ff0e'
]
]
]];
foreach ($array as $object) {
$id = $object->id;
$active = $object->active;
foreach ($object->events as $event) {
$event_id = $event->id;
$created_at = $event->created_at;
$visibility = $event->visibility;
$text = $event->text;
$author_id = $event->author_id;
echo "Event ID: $event_id, Created At: $created_at, Visibility: $visibility, Text: $text, Author ID: $author_id\n";
}
}

This will loop through the outer array and print out all the context for each event.

huangapple
  • 本文由 发表于 2023年2月6日 20:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定