PHP生成Json/LD如何隐藏数字数组键

bnl4lu3b  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(76)

我试图添加微数据到我的网站。有JSON数组。但它是无效的,因为数组键。
我添加数组数据如下:

$data[] = $extra_array_data;

我会假设这不会让我得到数组键0,1,2,3,4等。
但是当它转换为JSON时,它会这样做。

{
    "@context": "https://schema.org",
    "@graph": {
        "0": {
            "@type": [
                "WebPage"
            ],
            "@id": "https://example.com/#website",
            "url": "https://example.com/",
            "name": "example.com",
            "description": null,
            "inLanguage": "nl"
        },
        "@type": [
            "WebPage"
        ],
        "@id": "https://example.com/#webpage",
        "url": "https://example.com/contact",
        "name": null,
        "isPartOf": {
            "@id": "https://example.com#website"
        },
        "datePublished": "2023-03-13T21:29:43.3600Z",
        "dateModified": "2023-03-13T21:29:43.3600Z",
        "description": "",
        "inLanguage": "nl-NL",
        "potentialAction": {
            "@type": "ReadAction",
            "target": [
                "https://example.com/contact"
            ]
        }
    }
}

我希望有人能告诉我我犯了什么错误。

$json_data      = json_encode($return_data, JSON_UNESCAPED_UNICODE);
omqzjyyz

omqzjyyz1#

如果你想让你的$extra_array_data替换现有的键,那么你需要合并数组:

$data = array_merge($data, $extra_array_data);
{
    "@context": "https://schema.org",
    "@graph": {
        "@type": [
            "WebPage"
        ],
        "@id": "https://example.com/#website",
        "url": "https://example.com/",
        "name": "example.com",
        "isPartOf": {
            "@id": "https://example.com#website"
        },
        "datePublished": "2023-03-13T21:29:43.3600Z",
        "dateModified": "2023-03-13T21:29:43.3600Z",
        "description": null,
        "inLanguage": "nl",
        "potentialAction": {
            "@type": "ReadAction",
            "target": [
                "https://example.com/contact"
            ]
        }
    }
}

相关问题