我有一个simpleXml对象,想要从对象中读取数据。我是php的新手。对象详细信息如下。我想读取一般名称和公司数组内的名称,即Korey Kay&合作伙伴。它的语法是什么?
SimpleXMLElement Object (
[@attributes] => Array ( [type] => array )
[project] => Array (
[0] => SimpleXMLElement Object (
[created-on] => 2008-07-18
[id] => 2257372
[last-changed-on] => 2010-05-27T22:28:29Z
[name] => *GENERAL
[status] => active
[company] => SimpleXMLElement Object (
[id] => 406952
[name] => Korey Kay & Partners
)
)
)
)
答案 0 :(得分:4)
The documentation offers some examples。我认为这是非常好的解释。
因为这是你的第一个问题;)在你的情况下,它会是这样的:
$projects = array();
$companies = array();
foreach($xml->project as $project) {
$projects[$project->id] = $project->name;
$companies[$project->company->id] = $project->company->name;
// and / or
echo 'Project ' . $project->name . ' has ID ' . $project->id . PHP_EOL;
echo 'Company ' . $project->company->name . ' has ID ' . $project->company->id . PHP_EOL;
}
PHP的文档非常好用imho。对于真正的基本元素,它们提供了很好的例子。我非常建议你阅读它!