Php 模型上的Laravel循环并更改为阵列
我正在尝试将模型更改为xml格式 我做了几乎所有的事情,但我仍然需要这个东西:Php 模型上的Laravel循环并更改为阵列,php,arrays,laravel,laravel-4,Php,Arrays,Laravel,Laravel 4,我正在尝试将模型更改为xml格式 我做了几乎所有的事情,但我仍然需要这个东西: $data = [ 'domain'=> $xmlDocument->domain, 'FirstDate' => $xmlDocument->dateAttribute->first_date, 'LastDate' => $xmlDocument->dateAttribute->last_dat
$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'=>[]
],
];
如您所见,MasterAttributes
为空
模型$xmlDocument
具有如下一对多关系:
$xmlDocument->masterInformaton->masterAttributes
'MasterAttributes' => [
$masterAttributes
],
$attributes = $xmlDocument->masterInformaton->masterAttributes;
foreach($attributes as $attribute) {
$data['MasterPage']['MasterAttributes'][] = [
'masterAttribute' => [
'name' => $attribute->name,
'xpath' => $attribute->xpath,
]
];
}
masterAttributes
是多个部分
我的问题
如何使用这些元素并将其传输到数组中,因此该数组中的每个元素如下所示:
'masterAttribute' => [
'name' => ...,
'xpath' => ...
]
换句话说,我将有许多数组,这些数组将被添加到我向u展示的代码中的空主属性键中
我希望我向你表明了我的观点,如果没有,请告诉我
更新
我所尝试的:
$masterAttributes = [];
foreach ($xmlDocument->masterInformation->masterAttributes as $attribute){
$masterAttribute = [
'Attribute' =>[
'defaultValue' => $attribute->defaultValue,
'name' => $attribute->attributeName->name,
'xpath' => $attribute->xpath
]
];
array_push($masterAttributes, $masterAttribute);
}
然后我把结果如下:
$xmlDocument->masterInformaton->masterAttributes
'MasterAttributes' => [
$masterAttributes
],
$attributes = $xmlDocument->masterInformaton->masterAttributes;
foreach($attributes as $attribute) {
$data['MasterPage']['MasterAttributes'][] = [
'masterAttribute' => [
'name' => $attribute->name,
'xpath' => $attribute->xpath,
]
];
}
但生成的xml是:
<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>
其中,$data
是我向您展示的最后一个数组,array\u to\u xml
是:
// 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
@watcher
回答了我一个问题,这是他/她的回答的结果
<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>
这是xpath
新xpath
新xpath值
尝试以下方法:
$xmlDocument->masterInformaton->masterAttributes
'MasterAttributes' => [
$masterAttributes
],
$attributes = $xmlDocument->masterInformaton->masterAttributes;
foreach($attributes as $attribute) {
$data['MasterPage']['MasterAttributes'][] = [
'masterAttribute' => [
'name' => $attribute->name,
'xpath' => $attribute->xpath,
]
];
}
这是假设主属性上的属性是name
和xpath
更新
$subnode = $xml_student_info->addChild("item$key");
这是xml生成中添加这些item0
节点的行。删除这些索引并没有帮助,因为您确实需要基于这些索引构建子树,但不希望直接将它们添加到XML文档中
我试着想出一个快速的解决方案,但结果不是那么快(我还发现我认为最初的答案是在您找到xml生成代码的地方)
更新2
尝试在生成xml时使用它,看看它是否满足您的需要。它基于@drzaus:
function simple_xmlify($arr, SimpleXMLElement $root = null, $el = 'x') {
// based on, among others https://stackoverflow.com/a/1397164/1037948
if(!isset($root) || null == $root) $root = new SimpleXMLElement('<' . $el . '/>');
if(is_array($arr)) {
foreach($arr as $k => $v) {
// special: attributes
if(is_string($k) && $k[0] == '@') $root->addAttribute(substr($k, 1),$v);
// normal: append
else {
if(is_numeric($k) && is_array($v)) {
foreach($v as $ik => $iv) {
simple_xmlify($iv, $root);
}
} else {
simple_xmlify($v, $root->addChild(
// fix 'invalid xml name' by prefixing numeric keys
is_numeric($k) ? 'n' . $k : $k)
);
}
}
}
} else {
$root[0] = $arr;
}
return $root;
}//-- fn simple_xmlify
函数simple\u xmlify($arr,simplexmlement$root=null,$el='x'){
//基于https://stackoverflow.com/a/1397164/1037948
如果(!isset($root)| | null=$root)$root=新的SimpleXMLElement(“”);
if(is_数组($arr)){
foreach($arr为$k=>$v){
//特殊:属性
if(is_string($k)&&$k[0]='@')$root->addAttribute(substr($k,1),$v);
//正常:追加
否则{
if(is_数值($k)和is_数组($v)){
foreach($v为$ik=>$iv){
简单化($iv,$root);
}
}否则{
简单化($v,$root->addChild(
//通过在数字键前加前缀修复“无效xml名称”
是数字($k)?'n'$k:$k)
);
}
}
}
}否则{
$root[0]=$arr;
}
返回$root;
}//--fn简单化
在看到您的答案之前,我刚刚更新了问题,似乎我的回答与您的答案很接近,让我测试一下,我会随时更新您的+1。我更新了一点答案,试图使其与您所说的匹配,您希望输出更好。我不知道,但答案尚未更新。你确定你有吗?是的,这只是一个小小的变化。我假设您正在使用foreach遍历数组以生成xml,我认为您获得这些额外键的原因是您不希望作为xml元素包含的数字索引。你能用你用来生成xml的代码更新这个问题吗?但是如何把你的结果放到我的最终数组中呢?