You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中如何向多维数组的嵌套itemListElement添加子数组

给JSON-LD的BreadcrumbList添加ListItem子数组的PHP实现

现有代码

echo '<script type="application/ld+json">';

$data = array(
    '@context' => 'https://schema.org',
    '@graph' => array(),
);

$data['@graph'][] = [
    "@type" => "ImageObject",
   
];

$data['@graph'][] = [
    "@type" => "BreadcrumbList",
    "itemListElement" => array(),
];

print_r(json_encode($data));

echo "</script>";

预期生成的BreadcrumbList结构

{
    "@type": "BreadcrumbList",
    "@id": "http:\/\/localhost\/#breadcrumb",
    "itemListElement": [{
        "@type": "ListItem",
        "position": "1",
        "item": {
            "@id": "http:\/\/localhost",
            "name": "Home"
        }
    }, {
        "@type": "ListItem",
        "position": "2",
        "item": {
            "@id": "link 2",
            "name": "Home"
        }
    }]
}

两种实现方式

方式1:初始化时直接构建完整结构

最直接的方式是在把BreadcrumbList加入@graph数组时,就填充好所有需要的字段:

echo '<script type="application/ld+json">';

$data = array(
    '@context' => 'https://schema.org',
    '@graph' => array(),
);

$data['@graph'][] = [
    "@type" => "ImageObject",
   
];

// 直接写入完整的BreadcrumbList数据
$data['@graph'][] = [
    "@type" => "BreadcrumbList",
    "@id" => "http://localhost/#breadcrumb",
    "itemListElement" => [
        [
            "@type" => "ListItem",
            "position" => "1",
            "item" => [
                "@id" => "http://localhost",
                "name" => "Home"
            ]
        ],
        [
            "@type" => "ListItem",
            "position" => "2",
            "item" => [
                "@id" => "link 2",
                "name" => "Home"
            ]
        ]
    ]
];

// 可选:用JSON_PRETTY_PRINT格式化输出,方便调试
echo json_encode($data, JSON_PRETTY_PRINT);

echo "</script>";

方式2:事后修改最后一个@graph元素

如果需要在初始化BreadcrumbList之后再补充数据,可以通过引用定位到@graph的最后一个元素进行修改:

echo '<script type="application/ld+json">';

$data = array(
    '@context' => 'https://schema.org',
    '@graph' => array(),
);

$data['@graph'][] = [
    "@type" => "ImageObject",
   
];

$data['@graph'][] = [
    "@type" => "BreadcrumbList",
    "itemListElement" => array(),
];

// 获取@graph最后一个元素的引用,直接修改原数组
$breadcrumb = &end($data['@graph']);
$breadcrumb['@id'] = "http://localhost/#breadcrumb";

// 逐个添加ListItem
$breadcrumb['itemListElement'][] = [
    "@type" => "ListItem",
    "position" => "1",
    "item" => [
        "@id" => "http://localhost",
        "name" => "Home"
    ]
];

$breadcrumb['itemListElement'][] = [
    "@type" => "ListItem",
    "position" => "2",
    "item" => [
        "@id" => "link 2",
        "name" => "Home"
    ]
];

echo json_encode($data, JSON_PRETTY_PRINT);

echo "</script>";

注意:使用&end()是为了拿到元素的引用,这样修改$breadcrumb时会直接同步到原数组;如果去掉&,修改的只是副本,原数组不会变化。另外,JSON_PRETTY_PRINT仅用于调试,生产环境可以移除该参数以减少输出体积。

内容的提问来源于stack exchange,提问作者Tpp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:20:30