好的,所以继续这个问题,使用api(lyris hq)我正在返回这个xml数据集
<DATASET>
<TYPE>success</TYPE>
<RECORD>
<DATA type="name" id="205199">Dev1</DATA>
<DATA type="messages">4</DATA>
<DATA type="last-sent">Jun. 26, 2012</DATA>
<DATA type="members">1</DATA>
<DATA type="status">active</DATA>
<DATA type="cache-time">2012-06-26T18:21:18+00:00</DATA>
</RECORD>
<RECORD>
<DATA type="name" id="206378">Dev2</DATA>
<DATA type="messages">1</DATA>
<DATA type="last-sent"></DATA>
<DATA type="members">2</DATA>
<DATA type="status">active</DATA>
<DATA type="cache-time">2012-05-31T14:47:12+00:00</DATA>
</RECORD>
<RECORD>
<DATA type="name" id="206379">Dev3</DATA>
<DATA type="messages">1</DATA>
<DATA type="last-sent"></DATA>
<DATA type="members">1</DATA>
<DATA type="status">active</DATA>
<DATA type="cache-time">2012-05-31T14:47:12+00:00</DATA>
</RECORD>
<RECORD>
<DATA type="name" id="206380">Dev4</DATA>
<DATA type="messages">1</DATA>
<DATA type="last-sent"></DATA>
<DATA type="members">1</DATA>
<DATA type="status">active</DATA>
<DATA type="cache-time">2012-05-31T14:47:12+00:00</DATA>
</RECORD>
</DATASET>
我尝试做的是使用SimpleXMLElement以递归方式使用它来创建一个数组
$response = array(
'{listid (id attribute from name element)}' => array(
'{other attribute type}' => '{value}'
),
);
使用dev1(第一条记录)的例子
$response = array(
'205199' => array(
'name' => 'Dev1',
'messages' => '4',
'last-sent' => 'Jun. 26, 2012',
'members' => '1',
'status' => 'active',
'cache-time' => '2012-06-26T18:21:18+00:00'
),
);
我和这里的网络团队的几位成员已经对我们进行了一段时间的讨论,并且没有在哪里,所以我希望我可以从荨麻疹的心中得到一些帮助,可以这么说lol
这是一个可怜的例子,说明我最后一次尝试使用我的结果明显失败
foreach ($responseobj->RECORD as $value) {
$returnarray[(string)$value->DATA['id']] = array();
foreach ($value->DATA as $k => $v) {
echo '<pre>';
print_r($v);
echo '</pre>';
$returnarray[(string)$v['id']] = array((string)$v['type'] => (string)$v,);
}
}
答案 0 :(得分:0)
<?php
$x = simplexml_load_file('example.xml');
$returnarray = array();
foreach($x->RECORD as $value){
$returnarray[(string)$value->DATA['id']] = array();
foreach ($value->DATA as $k => $v) {
$returnarray[(string)$value->DATA['id']][(string)$v['type']] = (string)$v;
}
}
var_dump($returnarray);