PHP: Multidimensional array
I have an array that looks like this:
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)
                             )
)
And so on, with many hours for today, and many for yesterday.
Now, I'm a bit lost on how to get the same hour for today and yesterday inside a foreach. For example, I have:
foreach ($Array as $key => $data) { 
//display today's hour
    //display yesterday's hour value
How can I get the values for all of them, line by line?
you have to nest 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 with that array above
foreach($array as $key => $arr){
  foreach($arr as $a_key => $a_arr){
    foreach($a_arr $b_key => $b_str){
      var_dump($b_str);
    }
  }
}
That will product 6 single lines of the value of hour for each one
How ever i think you have got a daft array setup there its just wasting memory for the fun of it,
It should be - holds the same data without a 2nd depth to your array
$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'
  )
);
What a strange array. Try this:
foreach ($Array as $key => $data) {
    foreach($data as $v){
        echo $key."'s hour: ".$v[hour];
    }
}
if count(array[today]) == count(array[yesterday]) then may be this helps:
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;
}
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论