将嵌套元素推入多维数组 [duplicate]

Push nested element into multidimensional array [duplicate]

提问人:Tpp 提问时间:1/21/2023 最后编辑:mickmackusaTpp 更新时间:1/23/2023 访问量:65

问:

我有这个数组

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>";

现在我想在最后一个 $data['@graph'][] 中添加另一个数组“itemListElement”并打印,但不知道该怎么做。

我期待

{
        "@type": "BreadcrumbList",
        "@id": "http:\/\/localhost\/#breadcrumb",
        "itemListElement": [{
            "@type": "ListItem",
            "position": "1",
            "item": {
                "@id": "http:\/\/localhost",
                "name": "Home"
            }
        }, {
            "@type": "ListItem",
            "position": "1",
            "item": {
                "@id": "link 2",
                "name": "Home"
            }
        }]
    } 
PHP 维数组 打印 嵌套

评论

0赞 zanderwar 1/21/2023
如果您不介意的话,请用您期望的结果示例来更新您的问题:)
0赞 vee 1/21/2023
"@type" => "BreadcrumbList",",语法不正确。
0赞 Tpp 1/21/2023
@vee已更新为

答:

0赞 zanderwar 1/21/2023 #1

您指的是嵌套数组。

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

$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'
            ]
        ]
    ]
];

print_r(json_encode($data));

echo "</script>";

祝你考试顺利

-1赞 Aaryan Mavani 1/21/2023 #2
<?php
  


// PHP program to creating two 
// dimensional associative array
$marks = array(
  
    // Ankit will act as key
    "Ankit" => array(
          
        // Subject and marks are
        // the key value pair
        "C" => 95,
        "DCO" => 85,
        "FOL" => 74,
    ),
          
    // Ram will act as key
    "Ram" => array(
          
        // Subject and marks are
        // the key value pair
        "C" => 78,
        "DCO" => 98,
        "FOL" => 46,
    ),
      
    // Anoop will act as key
    "Anoop" => array(
          
        // Subject and marks are
        // the key value pair
        "C" => 88,
        "DCO" => 46,
        "FOL" => 99,
    ),
);
  
echo "Display Marks: \n";
      
print_r($marks);

?>

**Output:**


Display Marks: 
Array
(
    [Ankit] => Array
        (
            [C] => 95
            [DCO] => 85
            [FOL] => 74
        )

    [Ram] => Array
        (
            [C] => 78
            [DCO] => 98
            [FOL] => 46
        )

    [Anoop] => Array
        (
            [C] => 88
            [DCO] => 46
            [FOL] => 99
        )`enter code here`

)