PHP将两个关联数组合并函数提高函数效率_php技巧
在foreach中循环查询数据代码量比较少,但是性能比较低,好点的解决办法是将id收集起来,用in一次性查询,但是这引发了数据结构不是我们用PHP自带的函数可以合并的,今天测试了一下:
使用下面的字节写的函数可以解决
从数据库中取出来的数据总是或多或少不符合我们心目中的数据结构,类似于下面的俩个数组,要形成SQL中类似于left join后两个数组合并:
$test1 = Array(
0 => Array(
'id' => 9478137,
'create_time' => 1394760724
),
1 => Array(
'id' => 9478138,
'create_time' => 1394760725
),
2 => Array(
'id' => 9478138,
'create_time' => 1394760725
)
);
$test2 = array(
0 => array(
'id' => 9478137,
'message' => 'love you'
),
1 => array(
'id' => 9478138,
'message' => 'miss you'
)
);
如果要将这两个数组,类似于sql中的left join 关联起来我们用什么函数呢?额我没有找见就自己写了
刚开始的时候,用的是嵌套循环:效率低下
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;
}
改进后的办法,使用数组下标,使用两次循环:形成类似于left join的方式
$test1 = Array(
0 => Array(
'id' => 9478137,
'create_time' => 1394760724
),
1 => Array(
'id' => 9478138,
'create_time' => 1394760725
),
2 => Array(
'id' => 9478138,
'create_time' => 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();
//使用数组下标的办法
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;
打印出来结果如下:
Array
(
[0] => Array
(
[id] => 9478137
[message] => love you
[create_time] => 1394760724
)
[1] => Array
(
[id] => 9478138
[message] => miss you
[create_time] => 1394760725
)
[2] => Array
(
[id] => 9478138
[message] => miss you
[create_time] => 1394760725
)
)
相当于left join了吧?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The differences between associative arrays and index arrays in PHP are: 1. Index arrays use numeric indexes, while associative arrays use string keys to identify and access elements; 2. The order of elements in an index array is the same as the order in which they are added to the array, while The order of elements in an associative array does not matter; 3. An indexed array can access array elements through a numerical index starting from 0, while an associative array can use string keys to access array elements; 4. The element keys of an indexed array start from the number 0 numeric index of an associative array whose element keys are strings.

In-depth discussion of PHP arrays: comprehensive analysis of multi-dimensional arrays, associative arrays, etc. Arrays in PHP are a very important data structure that can store multiple data items and access them through indexes. In PHP, arrays can be divided into different types such as indexed arrays, associative arrays, and multidimensional arrays. Each type has its own unique uses and characteristics. This article will delve into the various types of PHP arrays, including how to declare, access, traverse and operate arrays, and will provide specific code examples to help readers better understand. 1. Index array index number

PHP associative array is a special type of array in which each element is composed of a key and a corresponding value. Unlike ordinary arrays, the indexes of associative arrays are not arranged in numerical order, but using characters. Keys such as strings or integers are used to identify and access values. Associative arrays are widely used in various programming tasks in PHP, including data storage, form processing, database queries, etc.

Methods for summing associative arrays in PHP: 1. Use foreach loop; 2. Use array_sum() function; 3. Use array_reduce() function.

Fetching a row from a result set as an associative array using the PHP function "mysqli_fetch_assoc" In PHP, interacting with a database is a common task. When we execute a SELECT query and get the result set, we usually need to store the data in the result set into a PHP array for further processing. PHP provides multiple functions to process result sets, one of the commonly used functions is "mysqli_fetch_assoc". This function gets a row from the result set

With the continuous development of technology, web development has become more and more popular. PHP is one of the widely used web development languages. In the process of processing data, it is often necessary to compare the difference between two arrays. At this time we This can be achieved using the array_diff_assoc function in PHP. The array_diff_assoc function is used to compare the difference between two associative arrays and return a new array based on the key-value pairs. It returns an array containing all the parameters in the parameter array.

In PHP programming, array is a very common data type. PHP provides many built-in functions to process arrays, among which the arsort() function is a very useful function that can be used to sort associative arrays in descending order. This article will introduce the arsort() function in detail and give some practical examples. 1. What is the arsort() function? The arsort() function is a PHP built-in array sorting function that is used to sort an array in descending order by the value of an associative array. This function will not change

How to use and examples of associative arrays in PHP In PHP, an array is a very commonly used data type that is used to store multiple values and can be accessed by index or key. In many cases, using associative arrays is more convenient than using indexed arrays because associative arrays can use custom keys to access and manipulate the values in the array. An associative array is an array type that associates keys and values. Each key-value pair has a unique key in the array, and the corresponding value can be accessed and modified through the key. Here are some basic methods and examples of using associative arrays
