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