簡體   English   中英

我如何遍歷stdObject的數組

[英]How do I iterate through an stdObject's array

我嘗試遍歷以下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>';
}
}

我的猜測是,它遍歷鍵(何時是空白)而不是值遍歷我想要的結果。 我嘗試了一些似乎可行的方法。 如何循環避免使用[key]?

您需要改為訪問對象的values屬性,如下所示:

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

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM