我需要在PHP中使用Json文件,以便可以在其主要元素的特定键中进行迭代。
我有这个示例Json数据:
[{"A": [
{
"name": "object 1",
"code": "x001",
"date": "2018-01-01T23:00:00-0600"
},
{
"name": "object 2",
"code": "x002",
"date": "2018-01-01T23:00:00-0600"
},
{
"name": "object 3",
"code": "x003",
"date": "2018-01-01T23:00:00-0600"
}
]},{"B": [
{
"name": "object 1",
"code": "y001"
"date": "2018-01-01T23:00:00-0600"
},
{
"name": "object 2",
"code": "y002",
"date": "2018-01-01T23:00:00-0600"
}
]}]
据我所知,我可以像这样处理一个Json文件(在主键A和B中取消嵌套元素):
<?php
$url = './data.json';
$data = file_get_contents($url);
echo "<!--- File contents -->\n";
var_dump($data);
$contents = json_decode($data, true);
echo "<!--- Associative Array -->\n";
var_dump($contents);
echo "<!--- BEGIN -->";
foreach ($contents as $element) {
echo "Name: " . $element['name'] . "\n";
echo "Code: " . $element['code'] . "\n";
echo "Date: " . $element['date'] . "\n";
echo "- - - - - -\n";
}
echo "<!--- END -->";
?>
我想做的是在给定键内调用一个循环,例如,所有元素对应于 B 的循环:
{
"name": "object 1",
"code": "y001"
"date": "2018-01-01T23:00:00-0600"
},
{
"name": "object 2",
"code": "y002",
"date": "2018-01-01T23:00:00-0600"
}
可能是将这些元素提取到第二个关联数组或另一个建议的方法中...
预期结果必须是:
Name: object 1
Code: y001
Date: 2018-01-01T23:00:00-0600
- - - - - -
Name: object 2
Code: y002
Date: 2018-01-01T23:00:00-0600
- - - - - -
忽略 A 元素
答案 0 :(得分:2)
请尝试使用此一站式文件。您可以通过复制此代码自行尝试。
<?php
$data = '[{"A":[{"name": "object 1","code": "x001","date": "2018-01-01T23:00:00-0600"},{"name": "object 2","code": "x002","date": "2018-01-01T23:00:00-0600"},{"name": "object 3","code": "x003","date": "2018-01-01T23:00:00-0600"}]},{"B": [{"name": "object 1","code": "y001","date": "2018-01-01T23:00:00-0600"},{"name": "object 2","code": "y002","date": "2018-01-01T23:00:00-0600"}]}]';
// $url = './data.json';
// $data = file_get_contents($url);
echo "<!--- File contents -->\n";
$contents = json_decode($data, true);
echo "<!--- Associative Array -->\n";
echo "<pre>";
// var_dump($contents);
echo "</pre>";
echo "<!--- BEGIN -->";
echo "<pre>";
foreach ($contents as $element) {
if(!isset($element['B'])) continue;
foreach($element as $items){
foreach($items as $item){
echo "Name: " . $item['name'] . "\n";
echo "Code: " . $item['code'] . "\n";
echo "Date: " . $item['date'] . "\n";
echo "- - - - - -\n";
}
}
echo "- - - - - -\n";
}
echo "</pre>";
echo "<!--- END -->";
输出
Name: object 1
Code: y001
Date: 2018-01-01T23:00:00-0600
- - - - - -
Name: object 2
Code: y002
Date: 2018-01-01T23:00:00-0600
- - - - - -
- - - - - -
答案 1 :(得分:1)
此脚本可以帮助您设计和输出所需的字符串。它适用于$element["B"]
。您只需为数组的其他元素(例如foreach
)编写类似的$element["A"]
。
$url = './data.json';
$data = file_get_contents($url);
$output = "<!--- File contents -->\n";
$contents = json_decode($data, true);
$output .= "<!--- Associative Array -->\n";
$output .= "<!--- BEGIN -->\n";
foreach ($contents as $elements) {
if (!$elements["B"]) {continue;} // You might remove this and add other elements of your inputs such as $elements["A"]
foreach ($elements["B"] as $key => $element) {
$output .= "Name: " . $element['name'] . "\n";
$output .= "Code: " . $element['code'] . "\n";
$output .= "Date: " . $element['date'] . "\n";
$output .= "- - - - - -\n";
}
}
$output .= "<!--- END -->\n";
echo $output;
您可能不需要逐行回显,可以将其存储在$output
等变量中,也可以存储在echo $output;
末尾。
<!--- File contents -->
<!--- Associative Array -->
<!--- BEGIN -->
Name: object 1
Code: y001
Date: 2018-01-01T23:00:00-0600
- - - - - -
Name: object 2
Code: y002
Date: 2018-01-01T23:00:00-0600
- - - - - -
<!--- END -->
答案 2 :(得分:1)
您可以通过foreach()
循环来做到这一点
$array = json_decode($json,1);
//$array[1] for accessing contents of B
echo "<!--- BEGIN -->\n";
foreach($array[1] as $key=>$contents){
foreach ($contents as $element) {
echo "Name: " . $element['name'] . "\n";
echo "Code: " . $element['code'] . "\n";
echo "Date: " . $element['date'] . "\n";
echo "- - - - - -\n";
}
}
echo "<!--- END -->";
7.1.25-7.3.4的输出
<!--- BEGIN -->
Name: object 1
Code: y001
Date: 2018-01-01T23:00:00-0600
- - - - - -
Name: object 2
Code: y002
Date: 2018-01-01T23:00:00-0600
- - - - - -
<!--- END -->
工作演示: https://3v4l.org/ajnOG