我试图将simplexmlload字符串转换为数组,以便输出数组值。但是当我尝试输出数组值时,我收到此错误:
E_NOTICE:类型8 - 未定义的偏移:0 - 第55行
错误出现在这行代码中:echo $ array [0];
这是完整的代码:
<!DOCTYPE html>
<html>
<body>
<?php
$note=<<<XML
<Projects_Detail>
<Mr-Imran>
<projectBudget>125000</projectBudget>
<EmpNo>E100</EmpNo>
<DeptName>IT</DeptName>
<HourlyRate>625</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>120000</projectBudget>
<EmpNo>E101</EmpNo>
<DeptName>Database</DeptName>
<HourlyRate>500</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>135000</projectBudget>
<EmpNo>E102</EmpNo>
<DeptName>IT</DeptName>
<HourlyRate>550</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>115000</projectBudget>
<EmpNo>E103</EmpNo>
<DeptName>Testing</DeptName>
<HourlyRate>400</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>150000</projectBudget>
<EmpNo>E110</EmpNo>
<DeptName>Database</DeptName>
<HourlyRate>375</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>140000</projectBudget>
<EmpNo>E112</EmpNo>
<DeptName>Testing</DeptName>
<HourlyRate>425</HourlyRate>
</Mr-Imran>
</Projects_Detail>
XML;
$xml=simplexml_load_string($note);
$array = (array) $xml;
print_r(array_values($array));
echo $array[0];
?>
答案 0 :(得分:0)
您可以使用SimpleXMLElement::children()
方法:
<?php
$note=<<<XML
<Projects_Detail>
<Mr-Imran>
<projectBudget>125000</projectBudget>
<EmpNo>E100</EmpNo>
<DeptName>IT</DeptName>
<HourlyRate>625</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>120000</projectBudget>
<EmpNo>E101</EmpNo>
<DeptName>Database</DeptName>
<HourlyRate>500</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>135000</projectBudget>
<EmpNo>E102</EmpNo>
<DeptName>IT</DeptName>
<HourlyRate>550</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>115000</projectBudget>
<EmpNo>E103</EmpNo>
<DeptName>Testing</DeptName>
<HourlyRate>400</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>150000</projectBudget>
<EmpNo>E110</EmpNo>
<DeptName>Database</DeptName>
<HourlyRate>375</HourlyRate>
</Mr-Imran>
<Mr-Imran>
<projectBudget>140000</projectBudget>
<EmpNo>E112</EmpNo>
<DeptName>Testing</DeptName>
<HourlyRate>425</HourlyRate>
</Mr-Imran>
</Projects_Detail>
XML;
$xml = simplexml_load_string($note);
$children = $xml->children();
foreach($children as $child) {
print_r($child);
echo "<br/>";
}
您将收到以下输出:
SimpleXMLElement Object ( [projectBudget] => 125000 [EmpNo] => E100 [DeptName] => IT [HourlyRate] => 625 )
SimpleXMLElement Object ( [projectBudget] => 120000 [EmpNo] => E101 [DeptName] => Database [HourlyRate] => 500 )
SimpleXMLElement Object ( [projectBudget] => 135000 [EmpNo] => E102 [DeptName] => IT [HourlyRate] => 550 )
SimpleXMLElement Object ( [projectBudget] => 115000 [EmpNo] => E103 [DeptName] => Testing [HourlyRate] => 400 )
SimpleXMLElement Object ( [projectBudget] => 150000 [EmpNo] => E110 [DeptName] => Database [HourlyRate] => 375 )
SimpleXMLElement Object ( [projectBudget] => 140000 [EmpNo] => E112 [DeptName] => Testing [HourlyRate] => 425 )
将foreach
循环替换为upgraded
循环,我们使用array
- 强制转换,您可以将XML数据作为方便的数组:
foreach($children as $child) {
$childArray = (array)$child;
print_r($childArray);
echo "<br/>";
}
会给你以下输出:
Array ( [projectBudget] => 125000 [EmpNo] => E100 [DeptName] => IT [HourlyRate] => 625 )
Array ( [projectBudget] => 120000 [EmpNo] => E101 [DeptName] => Database [HourlyRate] => 500 )
Array ( [projectBudget] => 135000 [EmpNo] => E102 [DeptName] => IT [HourlyRate] => 550 )
Array ( [projectBudget] => 115000 [EmpNo] => E103 [DeptName] => Testing [HourlyRate] => 400 )
Array ( [projectBudget] => 150000 [EmpNo] => E110 [DeptName] => Database [HourlyRate] => 375 )
Array ( [projectBudget] => 140000 [EmpNo] => E112 [DeptName] => Testing [HourlyRate] => 425 )