【问题标题】:Parse XML array into php variables将 XML 数组解析为 php 变量
【发布时间】:2013-02-18 10:54:15
【问题描述】:

我有一个在一个站点上自动生成的 XML 提要,我正在尝试将其复制到另一个站点并将数据导入到 mysql 数据库中,但是由于我不熟悉数组,所以我遇到了困难分解成多个元素。

它的处理方式是否与标准数组的处理方式相同,或者是否每个都必须单独处理?输出的 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
    )

)

【问题讨论】:

  • 我不确定您的问题是什么。你不知道如何处理嵌套在其他数组中的数组吗?或者,您不知道如何从 XML 创建数组吗?

标签: php mysql xml arrays


【解决方案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'
)";

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 2012-05-22
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多