【问题标题】:Convert SimpleXMLElement Object while parsing from xml [duplicate]从 xml 解析时转换 SimpleXMLElement 对象 [重复]
【发布时间】:2013-03-20 12:37:52
【问题描述】:
$drivetracker = simplexml_load_file('geot_5980.xml');
$data = array();
foreach ($drivetracker->plays->qtr as $qtr):
foreach ($qtr->play as $myplay):
$hasball = $myplay['hasball'];
$play = $myplay['spot'];
$down = $myplay['down'];
$togo = $myplay['togo'];
$type = $myplay['type'];
$text = $myplay['text'];
array_push($data, array("text" => $text,                
            "spot" => $play,
            "ball" => $hasball,
            "togo" => $togo,
            "type" => $type,
            "drive"=> $drive)
               );
endforeach;
endforeach;
print_r($data);

我明白了……

[16] => Array
        (
            [text] => SimpleXMLElement Object
                (
                    [0] => Tanner, C. kick attempt good.
                )

            [spot] => SimpleXMLElement Object
                (
                    [0] => DU03
                )

            [ball] => SimpleXMLElement Object
                (
                    [0] => GT
                )

            [togo] => SimpleXMLElement Object
                (
                    [0] => 0
                )

            [type] => SimpleXMLElement Object
                (
                    [0] => X
                )

            [drive] => 7
        )

    [17] => Array
        (
            [text] => SimpleXMLElement Object
                (
                    [0] => Tanner, C. kickoff 51 yards to the DU14, Butler, L return 16 yards to the DU30 (Noble, D.).
                )

            [spot] => SimpleXMLElement Object
                (
                    [0] => GT35
                )

            [ball] => SimpleXMLElement Object
                (
                    [0] => GT
                )

            [togo] => SimpleXMLElement Object
                (
                    [0] => 0
                )

            [type] => SimpleXMLElement Object
                (
                    [0] => K
                )

            [drive] => 7
        )

)

我的问题是......我如何做到这一点,而不是

[text] => SimpleXMLElement Object
                (
                    [0] => Tanner, C. kick attempt good.
                )

我明白了

[text] = > "Tanner, C. kick attempt good."

【问题讨论】:

    标签: php xml simplexml


    【解决方案1】:

    代替:

    $hasball = $myplay['hasball'];
    $play = $myplay['spot'];
    $down = $myplay['down'];
    $togo = $myplay['togo'];
    $type = $myplay['type'];
    $text = $myplay['text'];
    

    你想打电话:

    $hasball = (string) $myplay['hasball'];
    $play = (string) $myplay['spot'];
    $down = (string) $myplay['down'];
    $togo = (string) $myplay['togo'];
    $type = (string) $myplay['type'];
    $text = (string) $myplay['text'];
    

    通过使用(string) 进行强制转换,它会将 XML 标记的内容作为字符串返回。

    【讨论】:

      【解决方案2】:

      如果您将SimpleXMLElement 类型转换为字符串,您将获得它的值。 所以这应该有效:

      array_push($data, array(
          "text" => (string)$text,                
          "spot" => (string)$play,
          "ball" => (string)$hasball,
          "togo" => (string)$togo,
          "type" => (string)$type,
          "drive"=> (string)$drive)
      );
      

      【讨论】:

        猜你喜欢
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-17
        • 2016-12-14
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多