PHP-通过命名键合并多维数组 [英] PHP - Merge-down multidimensional arrays by named key

查看:121
本文介绍了PHP-通过命名键合并多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组(其中包含多个数据库查询的结果-在示例中只有2个,但可以更多. 合并需要由指定的列(在本例中为 date 列)完成.我们将其称为 merge_by 列.

I have the following array (which contains results of multiple database queries - in the example only 2 but can be more. The merging needs to be done by a specified column (in this case the date column). Let's call this merge_by column.

 Array
(
[0] => Array
    (
        [0] => Array
            (
                [date] => 2011-01-17
                [col-1] => 58
                [col-2] => 54
            )

        [1] => Array
            (
                [date] => 2011-01-19
                [col-1] => 50
                [col-2] => 61
            )

        [2] => Array
            (
                [date] => 2011-01-20
                [col-1] => 44
                [col-2] => 22
            )

        [3] => Array
            (
                [date] => 
                [col-1] => 448
                [col-2] => 196

    )

[1] => Array
    (
        [0] => Array
            (
                [date] => 2011-01-17
                [col-3] => 1489
            )

        [1] => Array
            (
                [date] => 2011-01-18
                [col-3] => 1534 
            )

        [2] => Array
            (
                [date] => 
                [col-3] => 1534
            )

    )

)

我正在努力实现

Array
    (
        [0] => Array
            (
                [date] => 2011-01-17
                [col-1] => 58
                [col-2] => 54
                [col-3] => 1489
            )

        [1] => Array
            (
                [date] => 2011-01-18
                [col-1] =>
                [col-2] =>
                [col-3] => 1534
            )

        [2] => Array
            (
                [date] => 2011-01-19
                [col-1] => 50
                [col-2] => 61
                [col-3] =>
            )

        [3] => Array
            (
                [date] => 2011-01-20
                [col-1] => 44
                [col-2] => 22
                [col-3] =>
            )

        [4] => Array
            (
                [date] => 
                [col-1] => 448
                [col-2] => 196
                [col-3] => 1534

    )

注意事项:

  • merge_by 可以采用空或空字符串值
  • merge_by 在要合并的数组中将没有相同的值
  • 两个查询中的结果数将有所不同,并且 merge_by 值可能会有所不同,因此可能会遇到
    $arr[0][1][date] != $arr[1][1][date]
  • LE:除了 merge_by 列外,所有其他列都将互不相同,因此您将没有$arr[0][0][col-2]$arr[1][0][col-2]
  • merge_by can take null or empty string values
  • merge_by will not have same values in the arrays to be merged
  • the number of results in the 2 queries will differ and the merge_by values can differ, so one might encounter
    $arr[0][1][date] != $arr[1][1][date]
  • LE: except the merge_by column, all the other columns will differ from each other, so you won't have $arr[0][0][col-2] and $arr[1][0][col-2]

因此,此操作的目的是通过公用列合并某些查询的列.
来自太多列的太多数据库中的太多数据,仅凭SQL生成数据.我知道...预先计算值:) ...现在不是一种选择.

So the purpose of this is to merge the columns of some queries by a common column.
Too much data from too many databases from too many columns to make it from SQL only. I know... pre-calculate the values :) ... not an option now.

当它们的列数和索引匹配时,我设法使其工作.自从尝试了许多选项以来,现在我有一个版本不值得添加它作为示例.

I have managed to make it work when the number of columns and the indexes of these match. Have since tried many options so right now I have a version unworthy of adding it as example.

那么,有人对实现上述功能的功能有任何想法吗?

So does anyone any idea of a function that can achieve the above?

谢谢

推荐答案

首先,我将假定您的数据按日期排序,因为您可以在SQL中进行数据处理,并且在示例中也对数据进行了排序.您需要同时浏览所有集合,并随身携带.

First, I'm going to assume your data is sorted by date, since you can do it in SQL and it's sorted in your example. You need to walk through all of your sets at the same time, merging as you go.

$merged = array();
$i_first = 0;
$i_second = 0;

$count_first = count($data[0]);
$cound_second = count($data[1]);

while ($i_first < $count_first || $i_second < $count_second)
{
    // this comparison depends on what your merge_by is
    $diff = strtotime($data[$i_first]['date']) - strtotime($data[$i_second]['date']);

    if ($diff == 0)
    {
        $merged[] = array_merge($data[$i_first], $data[$i_second]);
        $i_first++;
        $i_second++;
    }
    elseif ($diff < 0) // first date is earlier
    {
        $i_first++;
    }
    else  // second date earlier
    {
        $i_second++;
    }
}

现在,您的$ merged数组应该具有所需的内容.请注意,此解决方案假定您没有一个块中有重复的日期行,否则它将覆盖现有结果.另外,如果需要,您可以扩展为具有两个以上的数据集.

Now your $merged array should have what you want. Note that this solution assumes you don't have duplicate date rows in one chunk, or it would overwrite the existing results. Also, you could expand to have more than two data sets if you want.

这篇关于PHP-通过命名键合并多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆