laravel PHP添加到相关的阵列产生的新节点 [英] laravel php add to associated array generates new nodes
问题描述
这是我的数组:
这是我最后的数组:
$数据= [
'域'=> $ xmlDocument->域,
FirstDate'=> $ xmlDocument-> dateAttribute-> FIRST_DATE,
LastDate'=> $ xmlDocument-> dateAttribute-> LAST_DATE,
'分类'= GT; $ xmlDocument->&类别 - GT;的名字,
操作= GT; $ xmlDocument-> websiteAction->的名字,
'源'= GT; $ xmlDocument->源 - >的名字,
日志文件'=> $ xmlDocument-> LOG_FILE,
DateAttribute'=> [
'名称'=> $ xmlDocument-> dateAttribute->的名字,
'地方'=> $ xmlDocument-> dateAttribute-> dateLocation->的名字,
DateFunction'=> $ xmlDocument-> dateAttribute-> dateFunction->名
]
'母版'=> [
MasterAttributes'=> ]
'集装箱'=>
的XPath => $ xmlDocument-> masterInformation->的XPath
]
'下一页'= GT; []
LinkAttribute'=> []
]
];
如你所见,在 MasterAttributes
键是一个空数组,我想它,并祝
什么我都试过
$属性= $ xmlDocument-> masterInformation-> masterAttributes;
的foreach($属性为$属性){
$ masterAttribute = [
'属性'=> [
'名'=> $属性 - > attributeName->的名字,
DEFAULT_VALUE'=> $属性 - > DEFAULT_VALUE
]
];
$数据['母版'] ['MasterAttributes'] [] = $ masterAttribute;
}
生成的XML是:
< MasterAttributes>
< item0>
<属性与GT;
<名称>浴室和LT; /名称>
< DEFAULT_VALUE>这是默认值< / DEFAULT_VALUE>
< /属性>
< / item0>
< ITEM1和GT;
<属性与GT;
<名称>的价格和LT; /名称>
<&DEFAULT_VALUE GT; VFD< / DEFAULT_VALUE>
< /属性>
< / item1的>
<&ITEM2 GT;
<属性与GT;
<名称>浴室和LT; /名称>
<&DEFAULT_VALUE GT;新的默认值和LT; / DEFAULT_VALUE>
< /属性>
< / ITEM2>
< / MasterAttributes>
请检查是否有提取 item0,项目1,ITEM2
如何删除它们吗?
更新1
从 @lowerends
第一次的答案后,我得到了这样的结果。
< MasterAttributes>
< Attribute_0>
<名称>浴室和LT; /名称>
< DEFAULT_VALUE>这是默认值< / DEFAULT_VALUE>
< / Attribute_0>
< Attribute_1>
<名称>的价格和LT; /名称>
<&DEFAULT_VALUE GT; VFD< / DEFAULT_VALUE>
< / Attribute_1>
< Attribute_2>
<名称>浴室和LT; /名称>
<&DEFAULT_VALUE GT;新的默认值和LT; / DEFAULT_VALUE>
< / Attribute_2>
< / MasterAttributes>
非常接近啥子我的需要,但我需要有属性
不是属性0
或属性1
更新2
我生成这样的XML:
$ XML =新的SimpleXMLElement(< XML版本= \\1.0 \\>< websiteInformation>< / websiteInformation>);
$这个 - > array_to_xml($数据,$ XML);
$ XML的> asXML(文件名.XmlDocument ::发现($ ID) - GT; IDXML);
其中, $数据
是finall数组,而 array_to_xml
的功能是:
公共职能array_to_xml($ student_info,&安培; $ xml_student_info){
的foreach($ student_info为$关键=> $值){
如果(is_array($值)){
如果(!is_numeric($键)){
$子节点= $ xml_student_info->的addChild($键);
$这个 - > array_to_xml(价值$,$子节点);
}
其他{
$子节点= $ xml_student_info->的addChild(项$键);
$这个 - > array_to_xml(价值$,$子节点);
}
}
其他{
$ xml_student_info->的addChild($钥匙,用htmlspecialchars($值));
}
}
}
您可以这样做:
$属性= $ xmlDocument-> masterInformation-> masterAttributes;
的foreach($属性为$关键=> $属性){
$ masterAttribute = [
'名'=> $属性 - > attributeName->的名字,
DEFAULT_VALUE'=> $属性 - > DEFAULT_VALUE
];
$数据['母版'] ['MasterAttributes'] [Attribute_ $键] = $ masterAttribute;
}
这将确保你没有重复的<属性>
中的元素你的< MasterAttributes>
元素。
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.
这篇关于laravel PHP添加到相关的阵列产生的新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!