显示 SimpleXML 时出错 object
Error is displaying SimpleXML object
<?xml version="1.0" encoding="UTF-8"?>
<TVchannel>
<month-name month="September">
<channel-name name="IT">
<title>Welcome to IT-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="PTG">
<title>Welcome to PTG-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="HR">
<title>Welcome to HR-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
</month-name>
<month-name month="October">
<channel-name name="IT">
<title>Welcome to IT-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="PTG">
<title>Welcome to PTG-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="HR">
<title>Welcome to HR-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
</month-name>
</TVchannel>
我有上面的XML数据文件。我试图回应 channel-name = "HR" 部分中的标题。所以,回显应该是 ' Welcome to HR-TV.
这是我的 php 代码
<?php
$picture_container = simplexml_load_file('data.xml');
echo $picture_container->[month-name[0]]->[channel-name[1]]->title;
?>
然而,当我 运行 这是得到这个错误
解析错误:语法错误,意外的“[”,需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或 C:\wamp64\www\POC 中的“{”或“$” - ITTV\logic.php 第 3 行
请帮忙
简单地说,尝试在使用方括号换行时删除 ->
运算符。类似如下:
<?php
$picture_container = simplexml_load_file('data.xml');
echo $picture_container["month-name"][0]["channel-name"][1]->title;
?>
echo $picture_container->{'month-name'}[0]->{'channel-name'}[1]->title->__toString();
顺便说一句,输出 'Welcome to HR-TV'.
应该是 {'channel-name'}[2]
这里有很多错误:
1. month-name 或 channel-name 不是常量。您不能将它们用作常量,即使它们是常量您也不能在声明常量或变量或任何方法或任何函数时使用“-”字母。 ( PHP 解释器将其理解为负操作。)
2. 如果您想使用 -!或者像这样的键,你应该使用这样的语法:
$something->{'keyword-name'}
因此您可以访问简单 XML 生成的属性,例如:
$picture_container->{'month-name'}[0]['channel_name'][1]->title;
<?xml version="1.0" encoding="UTF-8"?>
<TVchannel>
<month-name month="September">
<channel-name name="IT">
<title>Welcome to IT-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="PTG">
<title>Welcome to PTG-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="HR">
<title>Welcome to HR-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
</month-name>
<month-name month="October">
<channel-name name="IT">
<title>Welcome to IT-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="PTG">
<title>Welcome to PTG-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
<channel-name name="HR">
<title>Welcome to HR-TV</title>
<image-no-1></image-no-1>
<image-no-2></image-no-2>
<image-no-3></image-no-3>
<image-no-4></image-no-4>
<image-no-5></image-no-5>
</channel-name>
</month-name>
</TVchannel>
我有上面的XML数据文件。我试图回应 channel-name = "HR" 部分中的标题。所以,回显应该是 ' Welcome to HR-TV.
这是我的 php 代码
<?php
$picture_container = simplexml_load_file('data.xml');
echo $picture_container->[month-name[0]]->[channel-name[1]]->title;
?>
然而,当我 运行 这是得到这个错误 解析错误:语法错误,意外的“[”,需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或 C:\wamp64\www\POC 中的“{”或“$” - ITTV\logic.php 第 3 行
请帮忙
简单地说,尝试在使用方括号换行时删除 ->
运算符。类似如下:
<?php
$picture_container = simplexml_load_file('data.xml');
echo $picture_container["month-name"][0]["channel-name"][1]->title;
?>
echo $picture_container->{'month-name'}[0]->{'channel-name'}[1]->title->__toString();
顺便说一句,输出 'Welcome to HR-TV'.
应该是{'channel-name'}[2]
这里有很多错误:
1. month-name 或 channel-name 不是常量。您不能将它们用作常量,即使它们是常量您也不能在声明常量或变量或任何方法或任何函数时使用“-”字母。 ( PHP 解释器将其理解为负操作。)
2. 如果您想使用 -!或者像这样的键,你应该使用这样的语法:
$something->{'keyword-name'}
因此您可以访问简单 XML 生成的属性,例如:
$picture_container->{'month-name'}[0]['channel_name'][1]->title;