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 to be merged using the built-in functions of PHP. Today we tested it: the following byte writing function can solve the problem that the data obtained from the database is always more or less
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 to be merged using the built-in functions of PHP. Today we tested it: the following byte writing function can solve the problem that the data obtained from the database is always more or less
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 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:
PHP merges two joined arrays into functions --- improves Function efficiency $ 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.
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.
$ 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:
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?