PHP combines two joined arrays to improve function efficiency _ PHP Tutorial

Source: Internet
Author: User
PHP combines two joined arrays to improve function efficiency. In foreach, the amount of code for loop query data is relatively small, but the performance is relatively low. a better solution is to collect the id and use in for one-time query, however, this caused the data structure not because we had a small amount of data code in the foreach loop query, but the performance was relatively low. the better solution is to collect the id and use in one-time query, however, this caused the data structure not to be merged using the built-in functions of PHP. today we tested it:

Use the following byte writing function to solve the problem:

The data obtained from the database is always more or less inconsistent with our data structure. similar to the following two arrays, we need to form an SQL statement that is similar to the combination of the last two arrays after left join:

The code is as follows:


$ Test1 = Array (
0 => Array (
'Id' => 9478137,
'Create _ time' = & gt; 1394760724
),
1 => Array (
'Id' => 9478138,
'Create _ time' = & gt; 1394760725
),
2 => Array (
'Id' => 9478138,
'Create _ time' = & gt; 1394760725
)
);
$ Test2 = array (
0 => array (
'Id' => 9478137,
'Message' => 'Love you'
),
1 => array (
'Id' => 9478138,
'Message' => 'miss you'
)
);


If we want to associate these two arrays like left join in SQL, what functions do we use? I wrote it myself without looking for it.
At the beginning, nested loops were used: inefficient.

The code is as follows:


Function _ mergerArray ($ array1, $ array2, $ field1, $ field2 = ''){
$ Ret = array ();
Foreach ($ array1 as $ key1 => $ value1 ){
Foreach ($ array2 as $ key2 => $ value2 ){
If ($ value1 [$ field1] ==$ value2 [$ field2]) {
$ Ret [$ key1] = array_merge ($ value1, $ value2 );
}
}
}
Return $ ret;
}


The improved method is to use array subscript and two loops: to form a method similar to left join.

The code is as follows:


$ Test1 = Array (
0 => Array (
'Id' => 9478137,
'Create _ time' = & gt; 1394760724
),
1 => Array (
'Id' => 9478138,
'Create _ time' = & gt; 1394760725
),
2 => Array (
'Id' => 9478138,
'Create _ time' = & gt; 1394760725
)
);
$ Test2 = array (
0 => array (
'Id' => 9478137,
'Message' => 'Love you'
),
1 => array (
'Id' => 9478138,
'Message' => 'miss you'
)
);

Function _ mergerArray ($ array1, $ array2, $ field1, $ field2 = ''){
$ Ret = array ();

// How to use the array subscript
Foreach ($ array2 as $ key => $ value ){
$ Array3 [$ value [$ field1] = $ value;
}
Foreach ($ array1 as $ key => $ value ){
$ Ret [] = array_merge ($ array3 [$ value [$ field1], $ value );
}
Return $ ret;
}
$ Ret = _ mergerArray ($ test1, $ test2, 'id', 'id ');
Print_r ($ ret); exit;


The output is as follows:

The code is as follows:


Array
(
[0] => Array
(
[Id] = & gt; 9478137
[Message] => love you
[Create_time] = & gt; 1394760724
)
[1] => Array
(
[Id] = & gt; 9478138
[Message] => miss you
[Create_time] = & gt; 1394760725
)
[2] => Array
(
[Id] = & gt; 9478138
[Message] => miss you
[Create_time] = & gt; 1394760725
)
)


Is it equivalent to left join?

...

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.