提问人:Tpp 提问时间:1/21/2023 最后编辑:mickmackusaTpp 更新时间:1/23/2023 访问量:65
将嵌套元素推入多维数组 [duplicate]
Push nested element into multidimensional array [duplicate]
问:
我有这个数组
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"
}
}]
}
答:
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`
)
评论
"@type" => "BreadcrumbList",",
语法不正确。