打印关联数组 - 奇怪的输出

时间:2012-10-28 03:13:38

标签: php foreach associative-array

我是一个来自xml文档的关联数组...我正在输出我需要的结构所需的数据..

问题:在测试时会有回声/打印字符,我真的不确定为什么以及导致它的原因..

Array
(
[AR] => Array
    (
        [Pt Lincoln] => Array
            (
                [Track Desc] => Good
                [Mtg Id] => 675546624
                [Track Rating] => 2
                [Weather] => Fine
                [Abandoned] => N
                [1] => Array
                    (
                        [Race Name] => 3YO 0 - 64 HANDICAP
                        [Distance] => 1000
                        [1] => Array
                            (
                                [Number] => 1
                                [Runner Name] => HEAVENS SHOUT
                                [Barrier] => 5
                            )
...

php代码:

foreach($data_array as $id => $mtgid)
{
foreach($mtgid as $vname2 => $meet)
{
    echo $mtgid[$vname2]['Track Desc'];
    echo $mtgid[$vname2]['Mtg Id'];
    echo $mtgid[$vname2]['Track Rating'];
    echo $mtgid[$vname2]['Weather'];
    echo $mtgid[$vname2]['Abandoned'];


    foreach($meet as $race => $detail)
    {
        echo $meet[$race]['Race Name'];
        echo $meet[$race]['Distance'];

        foreach($detail as $horse => $selection)
        {
            echo $detail[$horse]['Number'];
            echo $detail[$horse]['Runner Name'];
            echo $detail[$horse]['Barrier'];
            echo "<br/>";

        }
        echo "<br/>";
    }   
}
}

?>

输出:

Good6755466242FineNGG
66
22
FF
NN
3YO 0 - 64 HANDICAP1000333
111
1HEAVENS SHOUT5
2HANK'S NEPHEW2
3RIFF RAFF1
4TIGARA'S GEM4
5THE HEADLINER8
6WHERE HEARTS LIE7
7SHEZA STAR3
8REDLEAF ROSE6

随机字符是GG6622FFNN

任何指导都会受到赞赏!

1 个答案:

答案 0 :(得分:0)

foreach($data_array as $id => $mtgid)
{
foreach($mtgid as $vname2 => $meet)
{

    echo $mtgid[$vname2]['Track Desc'];
    echo $mtgid[$vname2]['Track Rating'];
    echo $mtgid[$vname2]['Weather'];
    echo $mtgid[$vname2]['Abandoned'];


    for($i=0; $i<count($meet); $i++)
    {
        echo $meet[$i]['Race Name'];
        echo $meet[$i]['Distance'];

        for($j=0; $j<count($meet[$i]); $j++)
        {
            echo $meet[$i][$j]['Number'];
            echo $meet[$i][$j]['Runner Name'];
            echo $meet[$i][$j]['Barrier'];
            echo "<br/>";

        }
        echo "<br/>";
    }   
}
}