我正在编写一个 php 脚本来解析 kodi 生成/使用的电影 nfo 和电视 nfo 文件。
但是我在获取一些子属性时遇到了问题(不确定这是否是正确的术语。)
到目前为止,我正在使用 simplexml 来读取 nfo 文件并收集数据。
但是我很难获得数据的评级和评级子集。
我无法找出正确的上下文来获得我想要/需要的特定值。
<ratings>
<rating default="false" max="100" name="metacritic">
<value>46.0</value>
<votes>0</votes>
</rating>
<rating default="false" max="10" name="themoviedb">
<value>6.6</value>
<votes>1175</votes>
</rating>
<rating default="false" max="100" name="tomatometerallcritics">
<value>56.0</value>
<votes>36</votes>
</rating>
<rating default="false" max="10" name="tomatometeravgcritics">
<value>4.5</value>
<votes>36</votes>
</rating>
<rating default="true" max="10" name="imdb">
<value>6.6</value>
<votes>91750</votes>
</rating>
<rating default="false" max="10" name="trakt">
<value>7.2</value>
<votes>2629</votes>
</rating>
当我得到结果并解析它们并得到这个数组转储时,我不知道如何获取我想要的特定数据。
Array (
[rating] => Array (
[0] => Array (
[@attributes] => Array (
[default] => false
[max] => 10
[name] => themoviedb
)
[value] => 7.2
[votes] => 3716
)
[1] => Array (
[@attributes] => Array (
[default] => false
[max] => 100
[name] => tomatometerallcritics
)
[value] => 94.0
[votes] => 219
)
[2] => Array (
[@attributes] => Array (
[default] => true
[max] => 10
[name] => imdb
)
[value] => 7.3
[votes] => 130474
)
[3] => Array (
[@attributes] => Array (
[default] => false
[max] => 100
[name] => metacritic
)
[value] => 77.0
[votes] => 0
)
[4] => Array ( [@attributes] => Array ( [default] => false [max] => 10 [name] => tomatometeravgcritics ) [value] => 7.9 [votes] => 219 )
[5] => Array ( [@attributes] => Array ( [default] => false [max] => 10 [name] => trakt ) [value] => 7.5 [votes] => 8201 ) ) )
这是我在函数中用来抓取和解析 nfo 文件的代码。
// Read entire file into string
$xmlfile = file_get_contents($nfo);
// Convert xml string into an object
$new = simplexml_load_string($xmlfile);
// Convert into json
$con = json_encode($new);
// Convert into associative array
$newArr = json_decode($con, true);
我尝试了各种循环,但要么出错,要么什么也没给我。 Array_keys,array_values,我显然没有找到在 nfo 文件中获取这些字段的正确方法。
欢迎任何建议。
[编辑] 所以我想要的输出是这样的: 评级名称,投票。
由于每部电影 nfo 文件中的评分种类各不相同,只是为了能够获得那里的评分,以及他们对所选电影的投票。
[编辑]终于得到了每个评分我想要的所有数据的答案。我不得不对建议的代码进行一些细微的更改,这对我帮助很大!
// loop thru each rating
foreach($movie['rating'] as $a){
// loop thru each array length
for($i = 0; $i < count($a); $i++) {
// get all the properties of the rating
foreach($a[$i]['@attributes'] as $name => $attrib){
echo 'Name: ['.$name.']<Br/>';
echo 'Value: ['.$attrib.']<br/>';
echo 'Votes: ['.$a[$i]['value'].']<br/';
echo '<br/>';
}
}
}
这给了我输出:
姓名:[姓名] 值:[imdb] 票数:[6.4]
这是一个更长的列表,但现在我可以获得每个评级及其值/属性。
非常感谢!
$newArr = [
'rating'=> [
['@attributes'=>['default'=>'false','max'=>10,'name'=>'The Movie'], 'value'=>1, 'votes'=>5],
['@attributes'=>['default'=>'false','max'=>100,'name'=>'The Other Movie'], 'value'=>1, 'votes'=>5]
]
];
foreach($newArr['rating'] as $a){
foreach($a['@attributes'] as $name => $attrib){
echo 'attributes name = >' . $name . '< and its value is = >' . $attrib . "<\n";
}
}
结果
attributes name = >default< and its value is = >false<
attributes name = >max< and its value is = >10<
attributes name = >name< and its value is = >The Movie<
attributes name = >default< and its value is = >false<
attributes name = >max< and its value is = >100<
attributes name = >name< and its value is = >The Other Movie<
或者你也可以
$newArr = [
'rating'=> [
['@attributes'=>['default'=>'false','max'=>10,'name'=>'The Movie'], 'value'=>1, 'votes'=>5],
['@attributes'=>['default'=>'false','max'=>100,'name'=>'The Other Movie'], 'value'=>1, 'votes'=>15]
]
];
foreach($newArr['rating'] as $a){
echo 'max = ' . $a['@attributes']['max'].PHP_EOL;
echo 'name = ' . $a['@attributes']['name'].PHP_EOL;
// also easy access to votes and
echo 'Votes = ' . $a['votes'] . PHP_EOL;
}
给予
max = 10
name = The Movie
Votes = 5
max = 100
name = The Other Movie
Votes = 15