2012-12-02 62 views
0

我想將xml文件中的數據保存到我的數據庫中,並且在下面您可以看到我的php代碼,我嘗試創建一個關聯數組以將值存入以備後用。但是,實際的SimpleXMLElement對象是存儲的,而不是純粹的值。將數據從節點保存到數組

我有兩個問題在這裏:

  1. 我怎樣才能改變我的代碼,使得實際值(如數字或字符串)存儲,而不是SimpleXMLElement對象?
  2. 這是一個很好的存儲值的方法,以便以後用SQLLite存儲(我將使用它),或者我有哪些其他選項可以更好地工作?

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:從print_r的

<?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> 
    ... 

結果($生產者):

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... 
+0

你應該像這樣添加索引$ producer-> name [0]。儘管第二個人不知道什麼。 – povilasp

回答

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; 
} 
+0

'$ producer-> attributes();'返回關聯數組,所以使用'$ attr ['id']' – GBD

+0

當然,@GBD。你是對的。 –

+0

很好,解決了我的問題。謝謝! – holyredbeard

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()呼叫 - 假設這是你的原意。