dongsi5381 2010-11-16 23:47
浏览 93
已采纳

在数组中显示数组?

I have an array inside an array and I'd like to simply print out the contents of it... the problem is what I'm trying doesn't work... ie

        foreach($retval as $k=>$v){

          if (is_array($v)){

            foreach($v as $l=>$w){

            echo $l . ' = ' . $w . '<br />';

            }

        } else {

          echo $k . ' = ' . $v . '<br />';

        }

Doing that however I end up with:

id = 2

name = Speakers

form_field = hidden

display_order = 0

groups = Array

So I'm not getting a foreach in that inside array.. what am I doing wrong? It seems actually that my code believes everything return is an array when I'm pretty sure that only 'groups' is an array.


the structure of the array looks like so:

array ( 0 => array ( 'id' => 2, 'name' => 'Speakers', 'form_field' => 'hidden', 'display_order' => '0', 'groups' => array ( 0 => array ( 'bit' => '1', 'name' => 'don', 'display_order' => '4', 'subscribers' => 8, ), 1 => array ( 'bit' => '2', 'name' => 'tyler', 'display_order' => '5', 'subscribers' => 0, ), 2 => array ( 'bit' => '4', 'name' => 'stephen', 'display_order' => '6', 'subscribers' => 0, ), 3 => array ( 'bit' => '8', 'name' => 'daniel', 'display_order' => '7', 'subscribers' => 0, ), 4 => array ( 'bit' => '16', 'name' => 'william', 'display_order' => '8', 'subscribers' => 0, ), 5 => array ( 'bit' => '32', 'name' => 'bobbys', 'display_order' => '9', 'subscribers' => 0, ), ), ), )

Long story short, I'm actually just trying to search this whole thing for say the name 'bobby' and get a simple true or false on whether that value exists anywhere in there.

  • 写回答

5条回答 默认 最新

  • douchu5131 2010-11-16 23:49
    关注

    You're assuming that arrays are only doubly-nested, when $retval[<something>]['groups'] is also an array. Use recursion to handle this instead of just adding levels upon levels.

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

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式