Php 在xml from字符串中随机设置属性值
文本文件中有一个字符串。Php 在xml from字符串中随机设置属性值,php,xml,dom,for-loop,Php,Xml,Dom,For Loop,文本文件中有一个字符串。 我正在尝试从该文件中获取文本,并将其推入xml属性值。 这是我的代码:- <?php $region = file_get_contents('activity.text'); $arr1 = explode(',',$region); $arr1[array_rand($arr1)]; $ran1 = $arr1; // create doctype $dom = new DOMDocument("1.0"); // display document in
我正在尝试从该文件中获取文本,并将其推入xml属性值。
这是我的代码:-
<?php
$region = file_get_contents('activity.text');
$arr1 = explode(',',$region);
$arr1[array_rand($arr1)];
$ran1 = $arr1;
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("products");
$dom->appendChild($root);
//for($i=0,$j=351;$i<count($arr1);$i++,$j++)
for($i=0;$i<2;$i++)
{
$pro_id = $dom->createElement("product_id");
$root -> appendChild($pro_id);
//append attribute ib product_id
$attpro = $dom->createAttribute("value");
$pro_id -> appendChild($attpro);
//append attribue value in product_id
$attval = $dom->createTextNode($i+1);
$attpro -> appendChild($attval);
// create child dist_region
$dist_region = $dom->createElement("dist_region");
$pro_id->appendChild($dist_region);
// create attribute node
$attreg = $dom->createAttribute("value");
$dist_region->appendChild($attreg);
// create attribute value node
$attval = $dom->createTextNode("$ran1[$i]");
$attreg->appendChild($attval);
}
echo $dom->saveXML();
?>
在这里,我尝试将字符串中的文本作为属性值随机推送到xml文件中,我的代码输出为:-<products>
<product_id value="1">
<dist_activity value="10066"/>
<dist_activity value="10066"/>
</product_id>
<product_id value="2">
<dist_activity value="10067"/>
<dist_activity value="10067"/>
</product_id>
</products>
我希望输出类似以下类型的内容:-
<?xml version="1.0"?>
<products>
<product_id value="1">
<dist_activity value="10066"/>
<dist_activity value="10069(any thing not same)"/>
</product_id>
<product_id value="2">
<dist_activity value="10067"/>
<dist_activity value="10072(any thing not same)"/>
</product_id>
</products>
从字符串中随机获取属性值谢谢 我认为您的问题更多的是将值按所希望的顺序排列,而不是添加XML元素。一些提示:
- 您可以使用
将整个数组随机化。看shuffle()
- 您可以使用
生成一组值,每个值包含两个值。看array\u chunk()
$values = explode(',', '10066,10067,10068,10069,10070,10071,10072,5');
shuffle($values);
$groups = array_chunk($values, 2);
print_r($groups);
$count = count($groups);
foreach ($groups as $index => $group)
{
$last = $index+1 === $count;
printf(" %s─ product_id: %d\n", $last ? '└' : '├', $index + 1);
foreach ($group as $subindex => $value)
{
printf(" %s %s─ dist_activity: %s\n", $last ? ' ' : '│', $subindex ? '└' : '├', $value);
}
if (!$last) print(" │\n");
}
输出:
Array
(
[0] => Array
(
[0] => 10069
[1] => 10068
)
[1] => Array
(
[0] => 10072
[1] => 10067
)
[2] => Array
(
[0] => 5
[1] => 10070
)
[3] => Array
(
[0] => 10071
[1] => 10066
)
)
├─ product_id: 1
│ ├─ dist_activity: 10069
│ └─ dist_activity: 10068
│
├─ product_id: 2
│ ├─ dist_activity: 10072
│ └─ dist_activity: 10067
│
├─ product_id: 3
│ ├─ dist_activity: 5
│ └─ dist_activity: 10070
│
└─ product_id: 4
├─ dist_activity: 10071
└─ dist_activity: 10066
Array
(
[0] => Array
(
[0] => 10069
[1] => 10068
)
[1] => Array
(
[0] => 10072
[1] => 10067
)
[2] => Array
(
[0] => 5
[1] => 10070
)
[3] => Array
(
[0] => 10071
[1] => 10066
)
)
├─ product_id: 1
│ ├─ dist_activity: 10069
│ └─ dist_activity: 10068
│
├─ product_id: 2
│ ├─ dist_activity: 10072
│ └─ dist_activity: 10067
│
├─ product_id: 3
│ ├─ dist_activity: 5
│ └─ dist_activity: 10070
│
└─ product_id: 4
├─ dist_activity: 10071
└─ dist_activity: 10066