0
$drivetracker = simplexml_load_file('geot_5980.xml');
$data = array();
foreach ($drivetracker->plays->qtr as $qtr):
    foreach ($qtr->play as $myplay):
    $hasball = (string)$myplay['hasball'];
        $play = (string)$myplay['spot'];
        $down = (string)$myplay['down'];
    $togo = (string)$myplay['togo'];
    $type = (string)$myplay['type'];
    $text = (string)$myplay['text'];
        if($drive == $driveid){
                array_push($data, array("text" => $text,               
                "spot" => $play,
                "ball" => $hasball,
                "togo" => $togo,
                "type" => $type,
                "drive"=> $drive)
           );
            }
    }
    endforeach;
endforeach;

echo json_encode($data); 

如果我print_r($data)得到,

Array
(
    [0] => Array
        (
            [text] => Godhigh, R. rush for 2 yards to the GT27 (Canty, W).
            [spot] => GT25
            [ball] => GT
            [togo] => 10
            [type] => R
            [drive] => 7
        )

    [1] => Array
        (
            [text] => Days, S. rush for 5 yards to the GT32 (Woodruff, J;Brown, Ky).
            [spot] => GT27
            [ball] => GT
            [togo] => 8
            [type] => R
            [drive] => 7
        )

    [2] => Array
        (
            [text] => Sims, D. rush for 6 yards to the GT38, 1ST DOWN GT (Anunike, K;Sarmiento, S).
            [spot] => GT32
            [ball] => GT
            [togo] => 3
            [type] => R
            [drive] => 7
        )

    [3] => Array
        (
            [text] => Godhigh, R. rush for 18 yards to the DU44, 1ST DOWN GT (Cockrell, R).
            [spot] => GT38
            [ball] => GT
            [togo] => 10
            [type] => R
            [drive] => 7
        )

    [4] => Array
        (
            [text] => Lee, V. pass complete to Greene, J. for 5 yards to the DU39 (Foster, T).
            [spot] => DU44
            [ball] => GT
            [togo] => 10
            [type] => P
            [drive] => 7
        )

    [5] => Array
        (
            [text] => Laskey, Z. rush for 9 yards to the DU35 (Sink, N).
            [spot] => DU44
            [ball] => GT
            [togo] => 10
            [type] => R
            [drive] => 7
        )

    [6] => Array
        (
            [text] => Laskey, Z. rush for 5 yards to the DU30, 1ST DOWN GT (Canty, W;France, C).
            [spot] => DU35
            [ball] => GT
            [togo] => 1
            [type] => R
            [drive] => 7
        )

    [7] => Array
        (
            [text] => Laskey, Z. rush for 4 yards to the DU26 (Sink, N).
            [spot] => DU30
            [ball] => GT
            [togo] => 10
            [type] => R
            [drive] => 7
        )

    [8] => Array
        (
            [text] => Lee, V. rush for 3 yards to the DU23 (Brown, Ky).
            [spot] => DU26
            [ball] => GT
            [togo] => 6
            [type] => R
            [drive] => 7
        )

    [9] => Array
        (
            [text] => Laskey, Z. rush for 5 yards to the DU18, 1ST DOWN GT (Canty, W).
            [spot] => DU23
            [ball] => GT
            [togo] => 3
            [type] => R
            [drive] => 7
        )

    [10] => Array
        (
            [text] => Smith, O. rush for 8 yards to the DU10 (France, C).
            [spot] => DU18
            [ball] => GT
            [togo] => 10
            [type] => R
            [drive] => 7
        )

    [11] => Array
        (
            [text] => Lee, V. rush for 4 yards to the DU6, 1ST DOWN GT (France, C).
            [spot] => DU10
            [ball] => GT
            [togo] => 2
            [type] => R
            [drive] => 7
        )

    [12] => Array
        (
            [text] => Laskey, Z. rush for 3 yards to the DU3 (Helton, D;Woodruff, J).
            [spot] => DU06
            [ball] => GT
            [togo] => 0
            [type] => R
            [drive] => 7
        )

    [13] => Array
        (
            [text] => Laskey, Z. rush for no gain to the DU3 (Woodruff, J;Brown, Ky).
            [spot] => DU03
            [ball] => GT
            [togo] => 0
            [type] => R
            [drive] => 7
        )

    [14] => Array
        (
            [text] => Smith, O. rush for 1 yard to the DU2 (France, C).
            [spot] => DU03
            [ball] => GT
            [togo] => 0
            [type] => R
            [drive] => 7
        )

    [15] => Array
        (
            [text] => Lee, V. rush for 2 yards to the DU0, TOUCHDOWN, clock 04:34.
            [spot] => DU02
            [ball] => GT
            [togo] => 0
            [type] => R
            [drive] => 7
        )

    [16] => Array
        (
            [text] => Tanner, C. kick attempt good.
            [spot] => DU03
            [ball] => GT
            [togo] => 0
            [type] => X
            [drive] => 7
        )

    [17] => Array
        (
            [text] => Tanner, C. kickoff 51 yards to the DU14, Butler, L return 16 yards to the DU30 (Noble, D.).
            [spot] => GT35
            [ball] => GT
            [togo] => 0
            [type] => K
            [drive] => 7
        )

) => Array
        (
            [text] => Tanner, C. kickoff 51 yards to the DU14, Butler, L return 16 yards to the DU30 (Noble, D.).
            [spot] => GT35
            [ball] => GT
            [togo] => 0
            [type] => K
            [drive] => 7
        )

) 

但如果我这样做echo json_encode($data); ,它不会给我这种格式......(它给我空白)

[{"text":"","spot":"","ball":"","togo":"0","type":"","drive":""},{"text":"","spot":"","ball":"","togo":"0","type":"","drive":""},{"text":"","spot":"","ball":"","togo":"0","type":"","drive":""},{"text":"","spot":"","ball":"","togo":"0","type":"","drive":""},{"text":"","spot":"","ball":"","togo":"0","type":"","drive":""}]

$data 未通过 json 编码的可能原因是什么?

这是来自 print_r($data);

Array ( [0] => Array ( [text] => Godhigh, R. rush for 2 yards to the GT27 (Canty, W). [spot] => GT25 [ball] => GT [togo] => 10 [type] => R [drive] => 7 ) [1] => Array ( [text] => Days, S. rush for 5 yards to the GT32 (Woodruff, J;Brown, Ky). [spot] => GT27 [ball] => GT [togo] => 8 [type] => R [drive] => 7 ) [2] => Array ( [text] => Sims, D. rush for 6 yards to the GT38, 1ST DOWN GT (Anunike, K;Sarmiento, S). [spot] => GT32 [ball] => GT [togo] => 3 [type] => R [drive] => 7 ) [3] => Array ( [text] => Godhigh, R. rush for 18 yards to the DU44, 1ST DOWN GT (Cockrell, R). [spot] => GT38 [ball] => GT [togo] => 10 [type] => R [drive] => 7 ) [4] => Array ( [text] => Lee, V. pass complete to Greene, J. for 5 yards to the DU39 (Foster, T). [spot] => DU44 [ball] => GT [togo] => 10 [type] => P [drive] => 7 ) [5] => Array ( [text] => Laskey, Z. rush for 9 yards to the DU35 (Sink, N). [spot] => DU44 [ball] => GT [togo] => 10 [type] => R [drive] => 7 ) [6] => Array ( [text] => Laskey, Z. rush for 5 yards to the DU30, 1ST DOWN GT (Canty, W;France, C). [spot] => DU35 [ball] => GT [togo] => 1 [type] => R [drive] => 7 ) [7] => Array ( [text] => Laskey, Z. rush for 4 yards to the DU26 (Sink, N). [spot] => DU30 [ball] => GT [togo] => 10 [type] => R [drive] => 7 ) [8] => Array ( [text] => Lee, V. rush for 3 yards to the DU23 (Brown, Ky). [spot] => DU26 [ball] => GT [togo] => 6 [type] => R [drive] => 7 ) [9] => Array ( [text] => Laskey, Z. rush for 5 yards to the DU18, 1ST DOWN GT (Canty, W). [spot] => DU23 [ball] => GT [togo] => 3 [type] => R [drive] => 7 ) [10] => Array ( [text] => Smith, O. rush for 8 yards to the DU10 (France, C). [spot] => DU18 [ball] => GT [togo] => 10 [type] => R [drive] => 7 ) [11] => Array ( [text] => Lee, V. rush for 4 yards to the DU6, 1ST DOWN GT (France, C). [spot] => DU10 [ball] => GT [togo] => 2 [type] => R [drive] => 7 ) [12] => Array ( [text] => Laskey, Z. rush for 3 yards to the DU3 (Helton, D;Woodruff, J). [spot] => DU06 [ball] => GT [togo] => 0 [type] => R [drive] => 7 ) [13] => Array ( [text] => Laskey, Z. rush for no gain to the DU3 (Woodruff, J;Brown, Ky). [spot] => DU03 [ball] => GT [togo] => 0 [type] => R [drive] => 7 ) [14] => Array ( [text] => Smith, O. rush for 1 yard to the DU2 (France, C). [spot] => DU03 [ball] => GT [togo] => 0 [type] => R [drive] => 7 ) [15] => Array ( [text] => Lee, V. rush for 2 yards to the DU0, TOUCHDOWN, clock 04:34. [spot] => DU02 [ball] => GT [togo] => 0 [type] => R [drive] => 7 ) [16] => Array ( [text] => Tanner, C. kick attempt good. [spot] => DU03 [ball] => GT [togo] => 0 [type] => X [drive] => 7 ) [17] => Array ( [text] => Tanner, C. kickoff 51 yards to the DU14, Butler, L return 16 yards to the DU30 (Noble, D.). [spot] => GT35 [ball] => GT [togo] => 0 [type] => K [drive] => 7 ) )
4

0 回答 0