我有一个来自json API的嵌套数组,通常有零个,一个或两个数组。
我可以得到它,如果数组为空以显示" N / A",并且只显示数组的第一个([0])实例。
然而显示第二个阵列让我感到沮丧。我知道我需要使用foreach迭代 PosTags (见下文)数组,以便提取foreach循环体中的每个 Tag 值。但是,我无法理解如何正确地做到这一点。
我的JSON:
[
{
"SectionDetails": [
{
"Description": "Course Description",
"Departments": "Department",
"Credits": "3.00",
"Meetings": [
{
"DOW": "Th",
"Dates": "08-31-2017 to 12-08-2017",
"Times": "03:00 PM - 05:30 PM",
}
],
"PosTags": [
{
"Tag": "HART-ANC"
},
{
"Tag": "ARCH-ARCH"
}
]
}
]
}
]
当前的PHP:
$postag = "N/A";
if (isset($sectiondetails->{'PosTags'}))
{
if(!empty($sectiondetails->{'PosTags'})) {
$postag=$sectiondetails->{'PosTags'}[0]->{'Tag'};
}
}
运行foreach循环,如下所示"类stdClass的对象无法转换为字符串"在echo tag
所在的行:
if (isset($sectiondetails->{'PosTags'})) {
if(!empty($sectiondetails->{'PosTags'})) {
$postag=$sectiondetails->{'PosTags'};
foreach ($postag as $tag) {
echo $tag;
}
}
}
理想情况下,我希望显示HART-ANC,ARCH-ARCH。想法?