Problems with the Foreach Loop array

Source: Internet
Author: User
Problems with the Foreach Loop array
The $account array is as follows
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
)

)

I'm going to loop 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 [' 163 '], looping like $account [' Sohu ']

Loop the two-dimensional array, which is the high efficiency


------Solution--------------------
References:
Reference: Set your two-dimensional array contains n one array, one-dimensional array has m key values,
So complete the traversal example 1 time complexity
For O (n*m)
Example 2
O (m+m+m) (N) = "O (n*m)
Efficiency I think it should be the same.

Two loops
$t 1 = microtime (true);
$data = Array (' tencent ' =
...
Indeed, as the landlord said, the browser under example 2 of the efficiency ratio of 1 faster nearly a time, but I think the landlord gave example 1 test cases
$t 1 = microtime (true);
$data = Array (' tencent ' = = Array (' CADFDG ', ' dfdgg '), ' sina ' = = Array (' 111 ', ' 4654654 '));
Position should be swapped
Here's 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);
  • Related Article

    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.