php - 将数组数据插入mysql php

标签 php mysql arrays

我有 test.xml 文件

<propertyList date="2012-05-21-17:12:37" username="" password="">
<business modTime="2011-10-31-12:33:07" status="sold">
  <agentID>TEST</agentID>
  <uniqueID>92134</uniqueID>
  <listingAgent id="1">
    <name>Spiro Abelas</name>
    <telephone type="BH"></telephone>
    <telephone type="mobile">0414298899</telephone>
    <email>spiro@abelas.com.au</email>
  </listingAgent>
  <listingAgent id="2"></listingAgent>
  <address display="no">
    <state>NSW</state>
    <postcode>2203</postcode>
    <country>Australia</country>
  </address>
</business>
</propertyList>

我确实像这样完全分析了 XML

<?php
$xml = simplexml_load_file('test.xml');
foreach($xml as $key0 => $value){
echo "..1..[$key0] => $value";
foreach($value->attributes() as $attributeskey0 => $attributesvalue1){
echo "________[$attributeskey0] = $attributesvalue1";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value as $key => $value2){
echo "....2.....[$key] => $value2";
foreach($value2->attributes() as $attributeskey => $attributesvalue2){
echo "________[$attributeskey] = $attributesvalue2";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value2 as $key2 => $value3){
echo ".........3..........[$key2] => $value3";
foreach($value3->attributes() as $attributeskey2 => $attributesvalue3){
echo "________[$attributeskey2] = $attributesvalue3";
}
echo '<br />';
////////////////////////////////////////////////
}}
echo '<br />';
}
?>

获取这些输出

  ..1..[business] => ________[modTime] = 2011-10-31-12:33:07________[status] = sold
  ....2.....[agentID] => TEST
  ....2.....[uniqueID] => 92134
  ....2.....[listingAgent] => ________[id] = 1
  .........3..........[name] => Spiro Abelas
  .........3..........[telephone] => ________[type] = BH
  .........3..........[telephone] => 0414298899________[type] = mobile
  .........3..........[email] => spiro@abelas.com.au
  ....2.....[listingAgent] => ________[id] = 2
  ....2.....[address] => ________[display] = no
  .........3..........[state] => NSW
  .........3..........[postcode] => 2203
  .........3..........[country] => Australia

我想需要触发查询(现在它是静态的,但我想使其动态)

insert into xml(business,agentID,uniqueID,listingAgent,name,telephone,email,state,postcode,country)values('sold', 'TEST', 92134, 1, 'Spiro Abelas', 0414298899, 'spiro@abelas.com.au', 'NSW', '2203', 'Australia')

请指导如何从数组中检索数据并将数据插入表

最佳答案

以下代码会将您的 XML 转换为数组。

    $xml    = '<?xml version="1.0" encoding="utf-8"?>
        <propertyList date="2012-05-21-17:12:37" username="" password="">
        <business modTime="2011-10-31-12:33:07" status="sold">
          <agentID>TEST</agentID>
          <uniqueID>92134</uniqueID>
          <listingAgent id="1">
            <name>Spiro Abelas</name>
            <telephone type="BH"></telephone>
            <telephone type="mobile">0414298899</telephone>
            <email>spiro@abelas.com.au</email>
          </listingAgent>
          <listingAgent id="2"></listingAgent>
          <address display="no">
            <state>NSW</state>
            <postcode>2203</postcode>
            <country>Australia</country>
          </address>
        </business>
        </propertyList>
    ';
$xml   = simplexml_load_string($xml);
$json  = json_encode($xml);
$array = json_decode($json,TRUE);

您可以像下面一样执行上面的代码和prinf数据

print"<pre>"; 
print_r($array );
print_r($array['business']['listingAgent'] );
print"</pre>";

您可以处理 $array 并保存在表中。

关于php - 将数组数据插入mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798338/

相关文章:

php - PHP 中的 RestAPI 设计——创建对象的对象而不是对象数组

mysql - 仅从 3 表连接中选择重复项

c++ - MySql - QSqlError 未被捕获

mysql - DATE( ) 导致 SQL 查询花费太长时间

c - 将数组传递给函数——出了什么问题?

php - 具有多个数组的 Foreach 循环

php - 本地主机上的 netcat "Connection refused"

PHP/MySQL - 单击按钮更新数据库/运行 PHP 脚本

arrays - Excel UDF 接受范围和数组作为参数,如 'SUM'

php - 使用 php mysql 动态创建带有选项组的选项列表