2011-08-03 227 views
-1

我有一個數組,看起來像這樣:PHP:多維數組

Array ([today] => Array ([0] => Array ([hour] => 08/03/11 00:00) 
          [1] => Array ([hour] => 08/03/11 11:00) 
          [n] => Array ([hour] => 0xxxxxxxxx) 
         ) 
     [yesterday] => Array ([0] => Array ([hour] => 08/02/11 00:00) 
           [1] => Array ([hour] => 08/02/11 11:00) 
           [n] => Array ([hour] => 0xxxxxxxxx) 
          ) 
) 

等,與今天許多小時,許多昨天。

現在,我對如何在今天和昨天在同一個foreach中獲得相同的小時有點失落。例如,我有:

foreach ($Array as $key => $data) { 

//display today's hour //display yesterday's hour value

我怎樣才能通過行得到的值所有的人,行?

回答

0
foreach($array['today'] as $k=>$v){ 
    //use $v 
    //use $array['yesterday'][$key] 
    //ex. 
    print 'today'.$v['hour'].'; yesterday '.$array['yesterday'][$k]['hour'].EOLN; 
} 
+0

簡單和工程:) – luqita

+1

這不會工作,你的引用'$ key'當你沒有把它設置它應該是'$ k' –

+0

@MartinBarker,謝謝,修正 – RiaD

1

奇怪的數組。嘗試:

foreach ($Array as $key => $data) { 
    foreach($data as $v){ 
     echo $key."'s hour: ".$v[hour]; 
    } 
} 
2

你必須巢Forech

$array = array(
    'today'=>array(
    0 => Array ('hour' => '08/03/11 00:00'), 
    1 => Array ('hour' => '08/03/11 11:00'), 
    'n' => Array ('hour' => '0xxxxxxxxx') 
), 
    'yesterday'=>array(
    0 => Array ('hour' => '08/03/11 00:00'), 
    1 => Array ('hour' => '08/03/11 11:00'), 
    'n' => Array ('hour' => '0xxxxxxxxx') 
) 
); 

SO上面

foreach($array as $key => $arr){ 
    foreach($arr as $a_key => $a_arr){ 
    foreach($a_arr $b_key => $b_str){ 
     var_dump($b_str); 
    } 
    } 
} 

該數組它將小時的每個值的產物6單一線一個

如何我認爲你有一個愚蠢的陣列設置有它的樂趣只是浪費內存,

它應該是 - 沒有第二個深度相同的數據保存到您的陣列

$array = array(
    'today'=>array(
    0 => '08/03/11 00:00', 
    1 => '08/03/11 11:00', 
    'n' => '0xxxxxxxxx' 
), 
    'yesterday'=>array(
    0 => '08/03/11 00:00', 
    1 => '08/03/11 11:00', 
    'n' => '0xxxxxxxxx' 
) 
); 
1

如果count(數組[日])==計數(數組[昨日]),則可能是這會有所幫助:

for ($i=0,$cnt=count(array['today']); $i<$cnt; $i++) 
    echo $Array['today'][$i]['hour'] . ' : ' $Array['yesterday'][$i]['hour'] 
) 
+0

也應該array是好的指標(沒有跳過索引) – RiaD

+0

@RiaD是和許多其他的東西.. =) – Subdigger