doufen3134 2017-09-05 13:22
浏览 13
已采纳

多级数组 - 好像我在圈子里走来走去?

This is an example of the array I am working with

array(7) 
{ 
    ["ClassId"]=> int(26) 
    ["ClassName"]=> string(9) "Candidate" 
    ["Data"]=> array(1) 
    { 
        [0]=> array(8) 
        { 
            ["AppDataId"]=> int(17736) 
            ["FirstName"]=> string(4) "hano" 
            ["LastName"]=> string(11) "steenhuizen" 
            ["CvTxtField"]=> string(4) "coal" 
            ["Telephone"]=> string(6) "2345§" 
            ["Email"]=> string(27) "hano11aaaaa@steenhuizen.com" 
            ["Abstract"]=> string(16) "hano steenhuizen" 
            ["TimeStamp"]=> string(22) "2017-09-05 06:08:41+02" 
        } 
    } 
    ["RowCount"]=> int(1) 
    ["PageNumber"]=> int(1) 
    ["PageSize"]=> int(100) 
    ["QueryTime"]=> string(6) "0.009s" 
}

For the life of me, I just cannot loop this with a basic PHP foreach loop? $objApi contains the array above

echo '<table>';
foreach($objApi as $value)
    {
            echo '<tr><td>' . $value['FirstName'] . '</td></tr>';
    }

echo '</table>

I would love to understand the workings of the array better as for some reason I just cannot get it right.

  • 写回答

3条回答 默认 最新

  • dongwen3093 2017-09-05 13:52
    关注

    The arrays is a tree of values associated to a key, you can define the key and the values as you with, even, you can create a value of a array as another array. The only thing that you have to know is the structure that you array has, at the moment to iterate.

    For you example code, if you want to iterate the data result of your query, this is the way:

    foreach($row['Data'] as $row){
        foreach($row as $user){
            echo '<tr><td>'.$user['FirstName'].'</td></tr>';
        }
    }
    

    I access directly in the array key that has the values of your query

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?