2013-09-23 175 views
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

回答

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

?> 
+0

我不得不添加[ '機'〕中各變量的聲明。有沒有更好的方法在循環中包含「機器」數組 – TJnTN