Foreach loop array problems

Source: Internet
Author: User
Foreach loop Array problem $ account & nbsp; Array is as follows Array (& nbsp; [tencent] & nbsp ;=& gt; & nbsp; array & nbsp; & nb foreach loop Array
The $ account array is as follows:
Array
(
[Tencent] => Array
(
[0] => mylove_fish2008
[1] => caochengli_love
)

[Sina] => Array
(
[0] = & gt; 2935994540
[1] => 3177446321.
)

[163] => Array
(
[0] = & gt; 3395157578
[1] => 8589895537.
)

[Sohu] => Array
(
[0] = & gt; 1400582280
[1] => 1044720542.
)

)

I want to loop through this array now, there are two ways
1. foreach ($ account as $ key => $ rows)
{
Foreach ($ rows as $ val)
{
Echo $ val .'';
}
}

2. foreach ($ account ['Tencent '] as $ val)
{
Echo $ val .'';
}
$ Account ['sina '], $ account ['20140901'], $ account ['sohu ']

Which efficiency is high when two-dimensional arrays such as loops are used?
------ Solution --------------------
Reference:
Reference: set your two-dimensional array to contain N one-bit arrays, which contain M key values,
So the time complexity of a complete traversal is 1.
O (N * M)
Example 2
O (M + M) (N) = "O (N * M)
I think efficiency should be the same.

// Two loops
$ T1 = microtime (true );
$ Data = array ('Tencent '=> ......

As the landlord said, the efficiency ratio of browser Example 2 is nearly doubled, but I think the test case 1 provided by the landlord
$ T1 = microtime (true );
$ Data = array ('Tencent '=> array ('cadcdg', 'dfdgg'), 'sina '=> array ('20140901', '20140901 '));
Swap location
Below is my test code

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);    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.