$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']一样循环循环这种二维数组,哪种效率高点

解决方案 »

  1.   

    设你的二维数组包含N个一位数组,一维数组中有M个键值,
    那么完整的遍历一次例1时间复杂度
    为O(N*M)
    例2
    O(M+M+M)(N个)=》O(N*M)
    效率我觉得应该是一样的
      

  2.   


    <?php
    // 两个循环
    $t1 = microtime(true);
    $data = array('tencent' => array('cadfdg', 'dfdgg'), 'sina' => array('111', '4654654'));
    foreach ($data as $key => $rows){
        foreach ($rows as $val){
            echo $val . ' ';
        }
    }
    $t2 = microtime(true) - $t1;
    echo "运行时间为{$t2}".'<br>';// 一个循环
    $t3 = microtime(true);
    foreach($data['tencent'] as $val) {
        echo $val . ' ';
    }
    foreach($data['sina'] as $val) {
        echo $val . ' ';
    }$t4 = microtime(true) - $t3;
    if ($t2 > $t4){
        echo "t2>t4";
    } elseif($t2<$t4) {
        echo "t2<t4";
    } else {
        echo "t2=t4";
    }
    echo $t2-$t4;
    echo "运行时间为{$t4}<br>";我用这个程序测试了一下,在浏览器下执行第二种快点,在命令行下第一种快点
      

  3.   

    确实如楼主所说,浏览器下例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);    
    }  

    $runtime= new runtime(); 

    $data = array('tencent' => array('cadfdg', 'dfdgg'), 'sina' => array('111', '4654654'), '163' => array('111', '4654654'), 'sohu' => array('111', '4654654'));
    $runtime->start();  
    foreach ($data as $key => $rows){
    foreach ($rows as $val){
    echo $val . ' ';
    }
    }
    $runtime->stop();
    echo "页面执行时间: ".$runtime->spent()." 毫秒"."<br/>"; 

    $runtime->start();  
    //$data =array('tencent' => array('cadfdg', 'dfdgg'));
    foreach($data['tencent'] as $val) {
    echo $val . ' ';
    }
    //$data = array('sina' => array('111', '4654654'));
    foreach($data['sina'] as $val) {
    echo $val . ' ';
    }
    //$data = array('163' => array('111', '4654654'));
    foreach($data['163'] as $val) {
    echo $val . ' ';
    }
    //$data = array('sohu' => array('111', '4654654'));
    foreach($data['sohu'] as $val) {
    echo $val . ' ';
    }
    $runtime->stop();
    echo "页面执行时间: ".$runtime->spent()." 毫秒";