This is my array:
This is my final array:
$data = [
'domain'=> $xmlDocument->domain,
'FirstDate' => $xmlDocument->dateAttribute->first_date,
'LastDate' => $xmlDocument->dateAttribute->last_date,
'Category' => $xmlDocument->category->name,
'Action' => $xmlDocument->websiteAction->name,
'Source' => $xmlDocument->source->name,
'LogFile' => $xmlDocument->log_file,
'DateAttribute' => [
'Name' => $xmlDocument->dateAttribute->name,
'Place' => $xmlDocument->dateAttribute->dateLocation->name,
'DateFunction' => $xmlDocument->dateAttribute->dateFunction->name
],
'MasterPage' => [
'MasterAttributes' =>[
],
'Container'=>[
'xpath' => $xmlDocument->masterInformation->xpath
],
'NextPage' =>[],
'LinkAttribute'=>[]
],
];
as you see, the
MasterAttributes
key is an empty array and I want to fille it.What I have tried
$attributes = $xmlDocument->masterInformation->masterAttributes;
foreach($attributes as $attribute) {
$masterAttribute = [
'Attribute' => [
'name' => $attribute->attributeName->name,
'default_value' => $attribute->default_value
]
];
$data['MasterPage'] ['MasterAttributes'][] = $masterAttribute;
}
The generated xml is:
<MasterAttributes>
<item0>
<Attribute>
<name>bathroom</name>
<default_value>This is the default value</default_value>
</Attribute>
</item0>
<item1>
<Attribute>
<name>price</name>
<default_value>vfd</default_value>
</Attribute>
</item1>
<item2>
<Attribute>
<name>bathroom</name>
<default_value>new default value</default_value>
</Attribute>
</item2>
</MasterAttributes>
please check that there are extract
item0, item1, item2
how to remove them please?
Update 1
after the first answer from
@lowerends
, I got this result <MasterAttributes>
<Attribute_0>
<name>bathroom</name>
<default_value>This is the default value</default_value>
</Attribute_0>
<Attribute_1>
<name>price</name>
<default_value>vfd</default_value>
</Attribute_1>
<Attribute_2>
<name>bathroom</name>
<default_value>new default value</default_value>
</Attribute_2>
</MasterAttributes>
very close to waht I need but I need to have
Attribute
not Attribute 0
or Attribute 1
Update 2
I generated the xml like this:
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><websiteInformation></websiteInformation>");
$this->array_to_xml($data,$xml);
$xml->asXML("FileName".XmlDocument::find($id)->id.".xml");
where
$data
is the finall array, and array_to_xml
function is: public function array_to_xml($student_info, &$xml_student_info) {
foreach($student_info as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_student_info->addChild("$key");
$this->array_to_xml($value, $subnode);
}
else{
$subnode = $xml_student_info->addChild("item$key");
$this->array_to_xml($value, $subnode);
}
}
else {
$xml_student_info->addChild("$key",htmlspecialchars("$value"));
}
}
}
You can do this:
$attributes = $xmlDocument->masterInformation->masterAttributes;
foreach($attributes as $key => $attribute) {
$masterAttribute = [
'name' => $attribute->attributeName->name,
'default_value' => $attribute->default_value
];
$data['MasterPage']['MasterAttributes']["Attribute_$key"] = $masterAttribute;
}
This will make sure that you don't have duplicate
<Attribute>
elements in your <MasterAttributes>
element.
0 comments:
Post a Comment