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条)

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错