我有以下代码
公共函数returnArrayOfXmlElements(字符串$ xml_path,字符串$ element_key) {
// Variables
$this->simplexml_array = (array)$this->returnSimpleXmlObject($xml_path);
// Variables With Dependencies
$this->xml_elements = $this->simplexml_array[$element_key];
foreach ($this->xml_elements as $this->array_key => $this->array_value) {
$this->xml_elements_rectified[] = (array)$this->array_value;
}
return $this->xml_elements_rectified;
}
当我尝试这个时,
switch ($menu_name) {
case 'secondary':
// Variables
$this->menu_directory = constant('ENVIRONMENT_DIRECTORY_FRONTEND_MENU_SECONDARY');
$this->request_uri = (empty($_GET['request']) !== TRUE ? $_GET['request'] : constant('SETTING_TEMPLATE_DEFAULT'));
// Variables With Dependencies
$this->section_name = strtolower(substr($this->request_uri, 0, strpos($this->request_uri, '/')));
$this->menu_file = $this->section_name.'.xml';
$this->menu_data = $this->library_xml->returnArrayOfXmlElements($this->menu_directory.$this->menu_file, 'item');
echo '
<ul menu-secondary>
<li class="icon"><icon id="section-'.$this->section_name.'"></icon></li>
';
foreach ($this->menu_data as $this->array_key => $this->menu_information) {
// Variables
$this->item_hook = $this->menu_information['hook'];
$this->item_name = $this->menu_information['name'];
echo '<li><a href="'.$this->item_hook.'">'.$this->item_name.'</a></li>';
}
echo '
</ul>
';
break;
default:
// Variables
$this->menu_directory = constant('ENVIRONMENT_DIRECTORY_FRONTEND_MENU');
$this->menu_file = 'main.xml';
// Variables With Dependencies
$this->menu_data = $this->library_xml->returnArrayOfXmlElements($this->menu_directory.$this->menu_file, 'item');
echo '
<ul menu-main>
<li>polybotes</li>
';
foreach ($this->menu_data as $this->array_key => $this->menu_information) {
// Variables
$this->item_hook = $this->menu_information['hook'];
$this->item_name = $this->menu_information['name'];
echo '<li><a href="'.$this->item_hook.'">'.$this->item_name.'</a></li>';
}
echo '
</ul>
';
break;
}
}
辅助开关中的$ this-> menu_data包含$ xml1和$ xml2的数据。如何防止这种情况?取消设置$ xml1不能解决问题。
XML文件如下所示:
$ xml1:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<menu>
<item>
<name>Dashboard</name>
<hook>/dashboard/view</hook>
</item>
<item>
<name>Location</name>
<hook>/location/list</hook>
</item>
<item>
<name>Networks</name>
<hook>/network/list</hook>
</item>
<item>
<name>Devices</name>
<hook>/device/list</hook>
</item>
<item>
<name>Alarms & Events</name>
<hook>/alarm/list</hook>
</item>
</menu>
$ xml2:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<menu>
<item>
<name>Network List</name>
<hook>/network/list</hook>
</item>
<item>
<name>Add Network</name>
<hook>/network/add</hook>
</item>
</menu>
您能帮我还是指出正确的方向?预先感谢。
答案 0 :(得分:0)
在方法returnArrayOfXmlElements
中,您似乎总是使用$this->variableName
,这会引起问题,因为它将在方法调用之间保留数据。
除非您确实需要保留此数据,否则请尝试从每个变量中删除$this->
...
public function returnArrayOfXmlElements(string $xml_path, string $element_key) {
// Variables
$simplexml_array = (array)$this->returnSimpleXmlObject($xml_path);
// Variables With Dependencies
$xml_elements = $this->simplexml_array[$element_key];
$xml_elements_rectified = [];
foreach ($xml_elements as $this->array_key => $this->array_value) {
$xml_elements_rectified[] = (array)$this->array_value;
}
return $xml_elements_rectified;
}