0

在 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

4

1 回答 1

0

Untestet 在飞行中

 <?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";
      }

 ?>
于 2013-09-23T13:51:27.473 回答