gpt4 book ai didi

php - 将 SimpleXMLElement 对象数组导出到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 07:53:48 24 4
gpt4 key购买 nike

我有来自 Web 服务的 SOAP 响应,并已将 XML 数据提取为 SimpleXMLElement 。然后我迭代这个对象以提取我需要的各种字段并将它们保存到一个数组中,该数组变成 SimpleXMLElement 的数组对象。

我现在正在尝试将这些数据导出到 MySQL 数据库中,根据我的研究,这意味着将数组转换为字符串,然后使用 mysql_query("INSERT INTO (whatever) VALUES (whatever)"); 。我已经尝试过implodeserialize但两者都不起作用,我收到错误:

Fatal error:  Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'

这就是我从 SimpleXMLELement 创建的数组。看起来像:

Array
(
[0] => Array
(
[uid] => SimpleXMLElement Object
(
[0] => WOS:000238186400009
)

[journal] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => source
)

)

[publication] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => item
)

[0] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
)

[year] => 2006
[author1] => SimpleXMLElement Object
(
[0] => Young, RP
)

[address] => SimpleXMLElement Object
(
[0] => Cent Sci Lab, Sand Hutton, Yorks, England
)

[author2] => SimpleXMLElement Object
(
[0] => Davison, J
)

[author3] => SimpleXMLElement Object
(
[0] => Trewby, ID
)

[citations] => SimpleXMLElement Object
(
[@attributes] => Array
(
[local_count] => 15
[coll_id] => WOS
)

)

) ... etc ...
)

谁能帮我想办法把这些数据存入我的数据库吗?我需要将其更改为另一种格式吗?

最佳答案

您必须迭代数组才能创建一个用字符串而不是 SimpleXMLElement 填充的新数组,例如:

<?php

// your array (already built)
$arraySimpleXml = array(
"example1" => new SimpleXMLElement("<test>value</test>"),
"example2" => new SimpleXMLElement("<test>value2</test>")
);

// output array, to store in database
$result = array();

foreach($arraySimpleXml as $key => $simpleXml) {
$result[$key] = $simpleXml->asXML();
}

// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);

?>

关于php - 将 SimpleXMLElement 对象数组导出到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25663510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com