如何遍历stdObject的数组

时间:2015-09-20 21:05:12

标签: php

我尝试循环遍历以下stdClass对象数组。

Array
(
[0] => stdClass Object
    (
        [key] => 49
        [values] => Array
            (
                [0] => Kansas City Chiefs
                [1] => -3
                [2] => Denver Broncos
                [3] => 3
                [4] => 41
                [5] => 2015-09-18 00:25:00
            )

    )

[1] => stdClass Object
    (
        [key] => 50
        [values] => Array
            (
                [0] => Carolina Panthers
                [1] => -3.5
                [2] => Houston Texans
                [3] => 3.5
                [4] => 40
                [5] => 2015-09-20 17:00:00
            )

    )

[2] => stdClass Object
    (
        [key] => 51
        [values] => Array
            (
                [0] => New Orleans Saints
                [1] => -10
                [2] => Tampa Bay Buccaneers
                [3] => 10
                [4] => 47
                [5] => 2015-09-20 17:00:00
            )

    )

[3] => stdClass Object
    (
        [key] => 52
        [values] => Array
            (
                [0] => Pittsburgh Steelers
                [1] => -6
                [2] => San Francisco 49ers
                [3] => 6
                [4] => 45
                [5] => 2015-09-20 17:00:00
            )

    )

[4] => stdClass Object
    (
        [key] => 53
        [values] => Array
            (
                [0] => Minnesota Vikings
                [1] => -3
                [2] => Detroit Lions
                [3] => 3
                [4] => 43
                [5] => 2015-09-20 17:00:00
            )

    )

[5] => stdClass Object
    (
        [key] => 54
        [values] => Array
            (
                [0] => Buffalo Bills
                [1] => 1
                [2] => New England Patriots
                [3] => -1
                [4] => 45
                [5] => 2015-09-20 17:00:00
            )

    )

[6] => stdClass Object
    (
        [key] => 55
        [values] => Array
            (
                [0] => Chicago Bears
                [1] => 2
                [2] => Arizona Cardinals
                [3] => -2
                [4] => 45
                [5] => 2015-09-20 17:00:00
            )

    )

[7] => stdClass Object
    (
        [key] => 56
        [values] => Array
            (
                [0] => Cleveland Browns
                [1] => 1
                [2] => Tennessee Titans
                [3] => -1
                [4] => 41
                [5] => 2015-09-20 17:00:00
            )

    )

[8] => stdClass Object
    (
        [key] => 57
        [values] => Array
            (
                [0] => Cincinnati Bengals
                [1] => -3.5
                [2] => San Diego Chargers
                [3] => 3.5
                [4] => 46
                [5] => 2015-09-20 17:00:00
            )

    )

[9] => stdClass Object
    (
        [key] => 58
        [values] => Array
            (
                [0] => Washington Redskins
                [1] => 3.5
                [2] => St. Louis Rams
                [3] => -3.5
                [4] => 41
                [5] => 2015-09-20 17:00:00
            )

    )

[10] => stdClass Object
    (
        [key] => 59
        [values] => Array
            (
                [0] => New York Giants
                [1] => -2.5
                [2] => Atlanta Falcons
                [3] => 2.5
                [4] => 51
                [5] => 2015-09-20 17:00:00
            )

    )

[11] => stdClass Object
    (
        [key] => 60
        [values] => Array
            (
                [0] => Oakland Raiders
                [1] => 6
                [2] => Baltimore Ravens
                [3] => -6
                [4] => 43
                [5] => 2015-09-20 20:05:00
            )

    )

[12] => stdClass Object
    (
        [key] => 61
        [values] => Array
            (
                [0] => Jacksonville Jaguars
                [1] => 6
                [2] => Miami Dolphins
                [3] => -6
                [4] => 41
                [5] => 2015-09-20 20:05:00
            )

    )

[13] => stdClass Object
    (
        [key] => 62
        [values] => Array
            (
                [0] => Philadelphia Eagles
                [1] => -5
                [2] => Dallas Cowboys
                [3] => 5
                [4] => 55
                [5] => 2015-09-20 20:25:00
            )

    )

[14] => stdClass Object
    (
        [key] => 63
        [values] => Array
            (
                [0] => Green Bay Packers
                [1] => -3.5
                [2] => Seattle Seahawks
                [3] => 3.5
                [4] => 49
                [5] => 2015-09-21 00:30:00
            )

    )

[15] => stdClass Object
    (
        [key] => 64
        [values] => Array
            (
                [0] => Indianapolis Colts
                [1] => -7
                [2] => New York Jets
                [3] => 7
                [4] => 47
                [5] => 2015-09-22 00:30:00
            )

    )

)

当我使用以下语法时,它正确打印一行,一行空白

foreach($tableData as $obj)
{
foreach ($obj as $tr)
{

    echo '<tr><td>'.$mysqldate = date( 'g:ia - D', $phpdate =     strtotime($tr[5])- 60 * 60 * 5).'</td><td><strong>'.($i += 4).'</strong> '.$tr[0].'<br><br><strong>'.($iii += 4).'</strong> Over</td><td>'.$tr[1].'<br><br>'.$tr[4].'</td><td><strong>'.($ii += 4).'</strong> '.$tr[2].'<br><br><strong>'.($iiii += 4).'</strong> Under</td><td>'.$tr[3].'<br><br>'.$tr[4].'</td></tr>';
}
}

我的猜测是它通过键循环,当它出现空白的时候,而不是通过我得到我想要的结果的值。我尝试了一些似乎有用的东西。如何循环避开[键]?

2 个答案:

答案 0 :(得分:2)

您需要访问对象的values属性,如下所示:

foreach($tableData as $obj)
{
  $tr = $obj->values;
  echo ....
}

答案 1 :(得分:0)

试试这个:

foreach($tableData as $key => $val){
  $val2 = $val->values;
   echo $val[0];
}