遍历由关联数组组成的数组
Looping over an array that consists of associate arrays
我有一个如下所示的数组:
[
{
"positional_index":"1.3",
"sourceid":10409,
"language": "Malayalam",
"field_gita_10409_text": "Behold, O Teacher! this mighty army of the sons of Pandu, arrayed by the son of Drupada, thy wise disciple."
},
{
"positional_index":"1.2",
"sourceid":10409,
"language": "Devanagari",
"field_gita_10409_text": "1.1 Dhritarashtra said What did my people and the sons of Pandu do when they had assembled together eager for battle on the holy plain of Kurukshetra, O Sanjaya."
},
]
我应该使用 foreach 循环来访问每个键。例如,如果键 = field_gita_10409_text,我必须使用 php 中的 explode() 函数来提取值 10409 等
我不知道在这种情况下如何使用 foreach 循环,因为该数组包含 2 个关联数组。请告诉我。
循环内循环:
foreach ($array as $item){
foreach ($item as $key=>$value){
//do stuff
}
}
您需要内部循环来获取键和值。
$array = json_decode($json);
foreach ($array as $innerArray){
foreach ($innerArray as $key=>$value){
if($key == 'field_gita_10409_text'){
$exp = explode($value);
}
}
}
我有一个如下所示的数组:
[
{
"positional_index":"1.3",
"sourceid":10409,
"language": "Malayalam",
"field_gita_10409_text": "Behold, O Teacher! this mighty army of the sons of Pandu, arrayed by the son of Drupada, thy wise disciple."
},
{
"positional_index":"1.2",
"sourceid":10409,
"language": "Devanagari",
"field_gita_10409_text": "1.1 Dhritarashtra said What did my people and the sons of Pandu do when they had assembled together eager for battle on the holy plain of Kurukshetra, O Sanjaya."
},
]
我应该使用 foreach 循环来访问每个键。例如,如果键 = field_gita_10409_text,我必须使用 php 中的 explode() 函数来提取值 10409 等
我不知道在这种情况下如何使用 foreach 循环,因为该数组包含 2 个关联数组。请告诉我。
循环内循环:
foreach ($array as $item){
foreach ($item as $key=>$value){
//do stuff
}
}
您需要内部循环来获取键和值。
$array = json_decode($json);
foreach ($array as $innerArray){
foreach ($innerArray as $key=>$value){
if($key == 'field_gita_10409_text'){
$exp = explode($value);
}
}
}