您好,欢迎访问一九零五行业门户网

foreach循环数组有关问题

foreach循环数组问题
$account 数组如下
array
(
    [tencent] => array
        (
            [0] => mylove_fish2008
            [1] => caochengli_love
        )
[sina] => array
        (
            [0] => 2935994540
            [1] => 3177446321
        )
[163] => array
        (
            [0] => 3395157578
            [1] => 8589895537
        )
[sohu] => array
        (
            [0] => 1400582280
            [1] => 1044720542
        )
)
我现在要循环这个数组,有两种方法
1.foreach ($account as $key => $rows)
  {
      foreach ($rows as $val)
      {
          echo $val.' ';
       }
   }
2.foreach ($account['tencent'] as $val)
  {
     echo $val.' ';
  }
  $account['sina'],$account['163'],$account['sohu']一样循环
循环这种二维数组,哪种效率高点
------解决方案--------------------
引用:引用:设你的二维数组包含n个一位数组,一维数组中有m个键值,
那么完整的遍历一次例1时间复杂度
为o(n*m)
例2
o(m+m+m)(n个)=》o(n*m)
效率我觉得应该是一样的
a……
确实如楼主所说,浏览器下例2的效率比例1快了将近一倍的时间,不过我认为楼主给的例1测试用例
$t1 = microtime(true);
 $data = array('tencent' => array('cadfdg', 'dfdgg'), 'sina' => array('111', '4654654'));
应该互换位置
下面是我的测试代码
class runtime {  
public $starttime = 0; 
public $stoptime = 0;
function get_microtime(){     
list($usec, $sec) = explode(' ', microtime());      
return ((float)$usec + (float)$sec);   
}
function start(){   
$this->starttime = $this->get_microtime();   
 }
function stop(){    
$this->stoptime = $this->get_microtime();   
}
function spent(){    
return round(($this->stoptime - $this->starttime) * 1000, 4);    

其它类似信息

推荐信息