doutong6814 2015-04-12 14:48
浏览 23
已采纳

PHP数组 - 获取值

I have an array, the output of print_r is:

Array ( [0] => Array ( [ID] => 1 [col1] => 1 ) [1] => Array ( [ID] => 2 [col1] => 2 ) )

Could you help of kind of array it is? So that I could research more about it? What I want is to get ID and col1 values

I've tried to use foreach:

foreach($array_name as $key=>$value){
     print "$key holds $value
";
 }

The output I get is 0 holds Array 1 holds Array And I would simply like to get:

1 1
2 2
  • 写回答

1条回答 默认 最新

  • donglu2523 2015-04-12 14:51
    关注

    It's a multi dimensional array, or an array where each element is another array. So you'll need to loop twice. Try this to look at it:

    foreach($array_names as $arr)
    {
      foreach($arr as $key => $val)
      {
        print "$key = $val
    ";
      }
    }
    

    Or, to get your just added desired output, do this:

    foreach($array_names as $arr)
    {
      foreach($arr as $key => $val)
      {
        print "$val ";
      }
      print "
    ";
    }
    

    Or this:

    foreach($array_names as $arr)
    {
       print $arr['ID'] . " " . $arr['col1'] . "
    ";
    }
    

    or a few other ways but you should be getting the picture.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀