2
$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."
4

2 に答える 2

4

それ以外の:

$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 タグの内容を文字列として返します。

于 2013-03-29T18:53:47.937 に答える
1

a を文字列に型キャストするSimpleXMLElementと、その値が得られます。したがって、これは機能するはずです:

array_push($data, array(
    "text" => (string)$text,                
    "spot" => (string)$play,
    "ball" => (string)$hasball,
    "togo" => (string)$togo,
    "type" => (string)$type,
    "drive"=> (string)$drive)
);
于 2013-03-29T18:54:10.587 に答える