我有一个像这样的数组:
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
如何逐行获取它们的值?
必须嵌套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'
)
);
好奇怪的数组。试试这个:
foreach ($Array as $key => $data) {
foreach($data as $v){
echo $key."'s hour: ".$v[hour];
}
}
如果count(array[today]) == count(array[yesterday]),那么这可能会有所帮助:
for ($i=0,$cnt=count(array['today']); $i<$cnt; $i++)
echo $Array['today'][$i]['hour'] . ' : ' $Array['yesterday'][$i]['hour']
)
foreach($array['today'] as $k=>$v){
//use $v
//use $array['yesterday'][$key]
//ex.
print 'today'.$v['hour'].'; yesterday '.$array['yesterday'][$k]['hour'].EOLN;
}