I am trying to change my model to xml format.
I did almost everything but I still need this thing:
$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
is empty.
the model $xmlDocument
has a one to many relationship like this:
$xmlDocument->masterInformaton->masterAttributes
the masterAttributes
is the many part
My question
how to use that many and transfer it to array, so each element in that array is this:
'masterAttribute' => [
'name' => ...,
'xpath' => ...
]
In other words, I will have many arrays and these arrays will be added to the empty MasterAttribute key in the code I showed to u.
I hope I made my point to you, if not, kindly tell me.
Update
What I have tried:
$masterAttributes = [];
foreach ($xmlDocument->masterInformation->masterAttributes as $attribute){
$masterAttribute = [
'Attribute' =>[
'defaultValue' => $attribute->defaultValue,
'name' => $attribute->attributeName->name,
'xpath' => $attribute->xpath
]
];
array_push($masterAttributes, $masterAttribute);
}
and then I put the result like this:
'MasterAttributes' => [
$masterAttributes
],
but the generated xml is:
<MasterAttributes>
<item0>
<item0>
<Attribute>
<defaultValue />
<name>bathroom</name>
<xpath>this is the xpath</xpath>
</Attribute>
</item0>
<item1>
<Attribute>
<defaultValue />
<name>price</name>
<xpath>new xpath</xpath>
</Attribute>
</item1>
<item2>
<Attribute>
<defaultValue />
<name>bathroom</name>
<xpath>new xpath value</xpath>
</Attribute>
</item2>
</item0>
</MasterAttributes>
look that there is extra Item2
and item0
that I don't want
Update 2
The code to generate the xml is:
$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 final array I showed to you and array_to_xml
is:
// function defination to convert array to xml
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"));
}
}
}
Updadte 3
The @watcher
gave me an aswer and this is the result of his/her answer
<MasterAttributes>
<item0>
<masterAttribute>
<name />
<xpath>this is the xpath</xpath>
</masterAttribute>
</item0>
<item1>
<masterAttribute>
<name />
<xpath>new xpath</xpath>
</masterAttribute>
</item1>
<item2>
<masterAttribute>
<name />
<xpath>new xpath value</xpath>
</masterAttribute>
</item2>
</MasterAttributes>