2013-02-18 135 views
0

我有一個在一個站點上自動生成的XML源,我試圖將它複製到另一個並將數據導入到mysql數據庫中,但是,我遇到了困難,因爲我對分解成多個元素的數組不熟悉。將XML數組解析爲php變量

它是否得到處理與標準數組相同的方式,或每個都必須分別處理?輸出的XML示例如下所示;

[str] => Array 
    (
     [0] => 0 
     [1] => USD 
     [2] => USPS 
     [3] => 4228547 
     [4] => 486948677 
     [5] => 7 
     [6] => IndyGen3 
     [7] => 1 
     [8] => 8 units|8 
     [9] => N/A 
     [10] => 1 
     [11] => Unlimited Refill 
     [12] => Unlimited Refill 
     [13] => www 
     [14] => 1297081 
     [15] => unitACTIVE4228547 
     [16] => 2 
     [17] => 0 
     [18] => unit-486948677 
     [19] => unit 
     [20] => ACTIVE 
     [21] => unit 
     [22] => 1 
     [23] => 1 
     [24] => 47d 23h 24m 
     [25] => 2013-02-15T19:35:15.153Z 
    ) 

[float] => Array 
    (
     [0] => 94.88 
     [1] => 94.88 
    ) 

[date] => Array 
    (
     [0] => 2013-02-15T19:35:15.438Z 
     [1] => 2013-02-15T19:33:14Z 
     [2] => 2013-02-15T19:35:02Z 
     [3] => 2013-04-04T19:00:00Z 
    ) 

[arr] => Array 
    (
     [0] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [name] => fm_ship_rules 
        ) 

       [str] => Array 
        (
         [0] => 6,3,25.0,,2013-04-02T19:00:00Z 
         [1] => 6,7,11.95,,2013-03-22T19:00:00Z 
         [2] => 6,15,19.95,,2013-04-02T19:00:00Z 
         [3] => 6,16,19.95,,2013-04-02T19:00:00Z 
        ) 

      ) 

     [1] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [name] => fm_dm_rules_desc 
        ) 

       [str] => Array 
        (
         [0] => USPS,USPS Priority, 25.0,,2013-04-02T19:00:00Z 
         [1] => USPS,USPS Two Day,11.95,,2013-03-22T19:00:00Z 
         [2] => USPS,USPS International Priority Puerto Rico,19.95,,2013-04-02T19:00:00Z 
         [3] => USPS,USPS International Priority Canada,19.95,,2013-04-02T19:00:00Z 
        ) 

      ) 

    ) 

[int] => Array 
    (
     [0] => 8 
     [1] => 8 
     [2] => 1 
    ) 

+1

我不知道你的問題是什麼。你不知道如何處理嵌套在其他數組中的數組?或者,你不知道如何從XML創建一個數組? – Shimu 2013-02-18 11:05:32

回答

1

要訪問多維數組像你上面你可以使用下面的符號的一個方面的信息。假設你的主陣列叫做$arr

$sql = "INSERT INTO `mytable` 
(
    `currency`, 
    `postal_service`, 
    `date`, 
    `amount_1`, 
    `amount_2` 
) VALUES (
    '".$arr[str][1]."', 
    '".$arr[str][2]."', 
    '".$arr[date][0]."', 
    '".$arr[float][0]."', 
    '".$arr[float][1]."' 
)"; 

這是等價的:

$sql = "INSERT INTO `mytable` 
(
    `currency`, 
    `postal_service`, 
    `date`, 
    `amount_1`, 
    `amount_2` 
) VALUES (
    'USD', 
    'USPS', 
    '2013-02-15T19:35:15.438Z', 
    '94.88', 
    '94.88' 
)";