将数据从节点保存到阵列

时间:2012-12-02 18:26:04

标签: php database arrays simplexml

我想将数据从xml文件保存到我的数据库,下面你可以看到我的php代码,我试图创建一个关联数组,将值放入以供以后使用。但是,存储的是实际的SimpleXMLElement对象,而不仅仅是纯值。

我在这里有两个问题:

  1. 如何更改代码以便存储实际值(作为数字或字符串)而不是SimpleXMLElement对象?
  2. 这是一种存储值的好方法,以便以后使用SQLLite(我将使用)存储,或者我有哪些其他选项更好用?
  3. PHP:

    $theProducers = simplexml_load_file('sources/producers.xml');
    
    $i = 0;
    foreach ($theProducers->producer as $producer) {
        $producers['id'][$i] = $producer->attributes();
        $producers['name'][$i] = $producer->name;
        $producers['address'][$i] = $producer->address;
        $producers['zipcode'][$i] = $producer->zipcode;
        $producers['town'][$i] = $producer->town;
        $producers['url'][$i] = $producer->url;
        $producers['imgurl'][$i] = $producer->imgurl;
    
        $i += 1;
    }
    
    print_r($producers);  // outcome below
    

    producers.xml:

    <?xml version="1.0"?>
    <producers>
      <producer id="8">
        <name>Em&#xE5;mejeriet</name>
        <address>Grenv&#xE4;gen 1-3</address>
        <zipcode>577 39</zipcode>
        <town>Hultsfred</town>
        <url>http://www.emamejeriet.se</url>
        <imgurl>http://172.16.206.1/~thajo/1DV449/laboration01/producenter/images/ema.png</imgurl>
      </producer>
      <producer>
      ...
    
    来自print_r($ producer)的

    结果:

    Array (
    [id] => Array (
        [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 ) )
        [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 57 ) )
        [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 45 ) )
        [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 33 ) )
        [4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 16 ) )
        [5] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 41 ) )
        [6] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 38 ) )
        [7] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 40 ) )
        [8] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 56 ) ) )
    [name] => Array (
        [0] => SimpleXMLElement Object ( [0] => Emåmejeriet )
        [1] => SimpleXMLElement Object ( [0] => Ölands Örtagård AB )
        [2] => SimpleXMLElement Object ( [0] => Dövestads utegrisar & Gårdsbutik )
    ... and so on...
    

2 个答案:

答案 0 :(得分:2)

更改输出数组的结构:

foreach ($theProducers->producer as $producer) {
    $attr = $producer->attributes();
    $producers[$i]['id'] = (int)$attr[0];
    $producers[$i]['name'] = (string)$producer->name;
    $producers[$i]['address'] = (string)$producer->address;
    $producers[$i]['zipcode'] = (string)$producer->zipcode;
    $producers[$i]['town'] = (string)$producer->towbn;
    $producers[$i]['url'] = (string)$producer->url;
    $producers[$i]['imgurl'] = (string)$producer->imgurl;

    $i += 1;
}

答案 1 :(得分:1)

只需转换为适当的数据类型(例如,通常为string,但有时其他类型,如int,可能有意义):

$producers['id'][$i] = (string)$producer->attributes()['id'];
$producers['name'][$i] = (string)$producer->name;
$producers['address'][$i] = (string)$producer->address;
$producers['zipcode'][$i] = (string)$producer->zipcode;
$producers['town'][$i] = (string)$producer->town;
$producers['url'][$i] = (string)$producer->url;
$producers['imgurl'][$i] = (string)$producer->imgurl;

注意:我还在['id']电话中添加了->attributes() - 假设这是您的意图。