如何在PHP中循环通过SimpleXML对象
我有一个simpleXml对象,想从该对象读取数据,我是PHP新手,不太知道如何执行此操作。对象详细信息如下所示 我需要阅读[说明]和[小时]。谢谢如何在PHP中循环通过SimpleXML对象,php,simplexml,Php,Simplexml,我有一个simpleXml对象,想从该对象读取数据,我是PHP新手,不太知道如何执行此操作。对象详细信息如下所示 我需要阅读[说明]和[小时]。谢谢 SimpleXMLElement Object ( [@attributes] => Array ( [type] => array ) [time-entry] => Array ( [0] => SimpleXMLElement Object (
SimpleXMLElement Object (
[@attributes] => Array (
[type] => array
)
[time-entry] => Array (
[0] => SimpleXMLElement Object (
[date] => 2010-01-26
[description] => TCDM1 data management: sort & upload
[hours] => 1.0
[id] => 21753865
[person-id] => 350501
[project-id] => 4287373
[todo-item-id] => SimpleXMLElement Object (
[@attributes] => Array (
[type] => integer
[nil] => true
)
)
)
[1] => SimpleXMLElement Object (
[date] => 2010-01-27
[description] => PDCH1: HTML
[hours] => 0.25
[id] => 21782012
[person-id] => 1828493
[project-id] => 4249185
[todo-item-id] => SimpleXMLElement Object (
[@attributes] => Array (
[type] => integer
[nil] => true
)
)
) ) )
请帮帮我。我尝试了很多东西,但是没有正确的语法。阅读上面的内容确实很困难,如果你能给我们一个屏幕截图或给出行距和缩进,那就更好了 如果你有一个物体 $xml是一个simplexml对象,您可以访问如下元素
$xml->element.
它看起来像是时间输入的一个元素,在这种情况下,您可以这样循环:
foreach( $xml->{'time-entry'} as $time_entry ) {
echo $time_entry->description . "<br />\n";
}
foreach($xml->{'time-entry'}作为$time\u entry){
echo$time\u条目->说明。“
\n”;
}
如果问题中打印的对象位于$element
中,则以下内容将在两个时间条目
上循环,同时打印出各自的说明
和小时
值
foreach ($element->{'time-entry'} as $entry) {
$description = (string) $entry->description;
$hours = (string) $entry->hours;
printf("Description: %s\nHours: %s\n\n", $description, $hours);
}
输出类似于:
Description: TCDM1 data management: sort & upload NFP SubProducers list
Hours: 1.0
Description: PDCH1: HTML
Hours: 0.25
请注意,time entry
元素名称必须用花括号1作为字符串包装,否则$element->time entry
将尝试读取time
元素并减去名为entry
的常量值,这显然不是我们想要的。另外,description
和hours
值被转换为字符串(否则它们将是simplexmlement
类型的对象)
一,。看见
2.请参见抱歉,我对PHP不熟悉,我刚开始学习,所以我的概念来自C编程。我可能在这里做了过多的工作/编码。。。但我相信我会被人骂,然后学习 这将返回一个数组,该数组的布局很好,可以访问所有单个数据位,而无需处理SimpleXMLElement对象:
public $xmlFileInName = 'sample.xml';
public $errorMessage = "Failed to open file: ";
public function actionGetArray()
{
try {
$xml = file_exists($this->xmlFileInName) ?
simplexml_load_file($this->xmlFileInName) :
exit($this->errorMessage. $this->xmlFileInName);
$xmlArray=$this->arrayFromSxe ($xml);
echo var_dump($xmlArray);
}
catch(Exception $e) {
echo $e->getMessage();
}
}
public function arrayFromSxe($sxe)
{
$returnArray = array();
foreach ((array)$sxe as $key=>$value) {
if(is_array($value) && !$this->is_assoc($value)) {
$indies = array();
foreach($value as $secondkey=>$secondvalue)
$indies[$secondkey] = $this->arrayFromSxe($secondvalue);
$returnArray[$key] = $indies;
}
else {
if(is_object($value)) {
$returnArray[$key] = $this->arrayFromSxe($value);
} else {
$returnArray[$key] = $value;
}
}
}
return $returnArray;
}
function is_assoc($array) {
return (bool)count(array_filter(array_keys($array), 'is_string'));
}
请注意,对于多个xml标记,例如在您的示例中:
[time-entry] => Array (
[0] => SimpleXMLElement Object (
...
)
[1] => SimpleXMLElement Object (
...
)
为了在代码中解决这个问题,您必须了解XML文件的结构。
这将在调用此行后以数组形式返回:
$xmlArray=$this->arrayFromSxe ($xml);
其中包括:
[time-entry][0]
[time-entry][1]
因此,您的代码需要知道如何通过int索引器对所有时间项进行迭代,或者在任何情况下,如果它们是一个xml元素的多次出现,则需要知道如何进行迭代
如果我所说的没有意义,那么就忽略它,简单地观察和评估代码
希望这有帮助