我需要一些帮助。
我有这样的json结构:
Array
(
[_metadata] => Array
(
[count] => 3413
[next] => Array
(
[rel] => next
)
[last] => Array
(
[rel] => last
)
[self] => Array
(
[rel] => self
)
[shardsStatusResult] => Array
(
[partialResult] =>
)
)
[conversationHistoryRecords] => Array
(
[0] => Array
(
[info] => Array
(
[startTime] => 2021-02-25 02:57:52.451+0000
[startTimeL] => 1614221872451
[endTime] => 2021-02-25 13:39:24.013+0000
[endTimeL] => 1614260364013
[duration] => 38491562
[conversationId] => 847b98b7-qd87-490d-p875-43cfed34f122
[brandId] => 59448905
[latestAgentId] => 1482731814
[latestAgentNickname] => Marilia
[latestAgentFullName] => Marilia
[latestAgentLoginName] => Marilia
[1] => Array
(
[info] => Array
(
[startTime] => 2021-02-25 02:56:35.787+0000
[startTimeL] => 1614221795787
[endTime] => 2021-02-25 11:57:46.051+0000
[endTimeL] => 1614254266051
[duration] => 32470264
[conversationId] => 847b98b7-qd87-490d-p875-43cfed55f122
[brandId] => 59448905
[latestAgentId] => 1637379330
[latestAgentNickname] => Matheus
[latestAgentFullName] => Matheus
[latestAgentLoginName] => Matheus
[2] => Array
(
[info] => Array
(
[startTime] => 2021-02-25 02:56:18.950+0000
[startTimeL] => 1614221778950
[endTime] => 2021-02-25 02:57:37.865+0000
[endTimeL] => 1614221857865
[duration] => 78915
[conversationId] => 847b87b7-qd87-490d-p875-43cfed55f122
[brandId] => 59448905
[latestAgentId] => 1637379330
[latestAgentNickname] => Lucas
[latestAgentFullName] => Lucas
[latestAgentLoginName] => Lucas
我需要从[conversationHistoryRecords]带来数据->(信息)→所有索引数组([1],[2],[3])的[latestAgentId]…我在这里看到了一些案例和一些Youtube视频,但我没有找到任何方法来做到这一点。
我试过了:
foreach ($MensagensHistoricas['conversationHistoryRecords'] as $Nivel1 => $Result){
foreach ($Result['info'] as $Nivel2 => $Result2 ) {
print_r($Result2)
;}
;}
foreach ($MensagensHistoricas['conversationHistoryRecords'] as $Nivel1){
foreach($Nivel1['info'] as $Nivel2){
print_r($Nivel2['latestAgentId']);
;}
;}
但是我没有成功。有人能帮我一下吗?
您可以从每个数组中提取info
列,如下所示:
$records = array_column($MensagensHistoricas['conversationHistoryRecords'], 'info');
foreach($records as $record) {
print_r($record);
// or
echo $record['latestAgentId']; // etc...
}
如果你真的只对latestAgentId
感兴趣,不需要其他的,那么也提取它:
$agentids = array_column(array_column($MensagensHistoricas['conversationHistoryRecords'], 'info'), 'latestAgentId');
foreach($agentids as $id) {
echo $id;
}
这里没有什么神奇的,您只需要一次迭代和正确的成员路径来访问所需的属性:
<?php
$input = [
'conversationHistoryRecords' => [
[
'info' => [
'latestAgentId' => 1482731814
]
], [
'info' => [
'latestAgentId' => 1637379330
]
], [
'info' => [
'latestAgentId' => 1637379330
]
]
]
];
$output = [];
array_walk($input['conversationHistoryRecords'], function ($conversationHistoryRecords) use (&$output) {
$output[] = $conversationHistoryRecords['info']['latestAgentId'];
});
print_r($output);
明显的输出是:
Array
(
[0] => 1482731814
[1] => 1637379330
[2] => 1637379330
)