循环通过多维/关联数组

时间:2013-09-23 13:45:02

标签: php arrays parsing loops

寻找在PHP中循环遍历多维/关联数组的一些帮助。本质上,我需要循环并输出表中的某些键值。没有太多运气。

以下是我的数组示例。

$myarray = array( 
"body"                   => array(

"0"                      => array( 
  "machine"                 => array(
    "id"                     => "1",
    "name"                   => "macbookpro",
    "description"            => "laptop machine",
    "state"                  => "reserved",
    "state_since"            => "2013-08-28 12:05:00",
    "queue_snapshot"         => array(
      "time"                   => "2013-08-01 12:00:00",
      "jobs"                   => "450",
      "jobs_available"           => "90",
      "cputime_running"        => "00:01:00",
      "cputime_eligible"       => "00:90:00",
      "cputime_block"          => " 90:00:00",
    )
  )
),

"1"                    => array(
  "machine"                 => array(
    "id"                     => "2",
    "name"                   => "ipad",
    "description"            => "tablet machine",
    "state"                  => "available",
    "state_since"            => "2013-08-28 12:05:00", 
    "queue_snapshot"         => array(
      "time"                   => "2013-08-01 12:00:00",
      "jobs"                   => "50",
      "jobs_available"         => "20",
      "cputime_running"        => "00:05:00",
      "cputime_eligible"       => "00:12:00",
      "cputime_block"          => " 00:10:00",
    )
  )
)
));

我在这个数组中有一些值需要稍后访问,所以我只需要能够访问特定的值来创建这个表。需要像这样输出......

机器名称|说明|国家|工作|可用职位|

macbookpro笔记本电脑预留450 90

ipad平板电脑可用50 20

1 个答案:

答案 0 :(得分:0)

动态取消测试

 <?php
      if(isset($myarray['body']) && is_array($myarray['body']))
      foreach($myarray['body'] as $id=>$arr) {
         $name = $arr['name'];
         $description = $arr['description'];
         $jobs = $arr['queue_snapshot']['jobs'];
         $jobs_available = $arr['queue_snapshot']['jobs_available'];

         echo "<br>$name $description $jobs $jobs_available";
      }

 ?>